Reputation: 13
I am trying to create a simple drawing programme with a button that clears the canvas by deleting all shapes on it, however i am trying to use the delete() command that is built into tkinter but when i try and run it it says that delete is not defined.`What am I doing wrong?
from Tkinter import *
import Tkinter
root = Tk()
class PaintBox( Frame ):
def __init__( self ):
Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Color Draw v0.2.63.23 open closed beta (Still in Alpha) greenlight edition" )
self.master.geometry( "600x600" )
self.message = Label( self, text = "Drag the mouse SLOWLY to draw" )
self.message.pack( side = TOP )
self.myCanvas = Canvas( self )
self.myCanvas.pack( expand = YES, fill = BOTH )
self.bd = (2)
self.myCanvas.bind( "<B1-Motion>", self.paint )
def paint( self, event ):
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
self.myCanvas.create_rectangle( x1, y1, x2, y2, fill = "black", tags="box")
button = Button(root, text = "Clear", command = delete(box))
button.grid(row = 1, column = 0)
PaintBox().mainloop()
Here is the full traceback @mgilson:
ERROR: execution aborted
In [2]: %run C:/Users/en58522/Downloads/paintcolor.txt---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195 else:
196 filename = fname
--> 197 exec compile(scripttext, filename, 'exec') in glob, loc
198 else:
199 def execfile(fname, *where):
C:\Users\en58522\Downloads\paintcolor.txt in <module>()
3 root = Tk()
4
----> 5 class PaintBox( Frame ):
6 def __init__( self ):
7 Frame.__init__( self )
C:\Users\en58522\Downloads\paintcolor.txt in PaintBox()
24 self.myCanvas.create_rectangle( x1, y1, x2, y2, fill = "black", tags="box")
25
---> 26 button = Button(root, text = "Clear", command = delete(box))
27 button.grid(row = 1, column = 0)
28
NameError: name 'delete' is not defined
Upvotes: 0
Views: 2114
Reputation: 309831
Here is some working code to get you started:
from Tkinter import *
import Tkinter
root = Tk()
class PaintBox( Frame ):
def __init__( self, root ):
Frame.__init__( self, root )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Color Draw v0.2.63.23 open closed beta (Still in Alpha) greenlight edition" )
self.master.geometry( "600x600" )
self.message = Label( self, text = "Drag the mouse SLOWLY to draw" )
self.message.pack( side = TOP )
self.myCanvas = Canvas( self )
self.myCanvas.pack( expand = YES, fill = BOTH )
self.bd = (2)
self.myCanvas.bind( "<B1-Motion>", self.paint )
button = Button(root, text = "Clear", command = self.delete)
button.pack()
def paint( self, event ):
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
self.myCanvas.create_rectangle( x1, y1, x2, y2, fill = "black", tags="box")
def delete(self):
items = self.myCanvas.find_all()
for item in items:
self.myCanvas.delete(item)
PaintBox(root).mainloop()
I've tried to stay true to your original code as much as possible.
A few things to notice...
.pack
the button because using .grid
and .pack
in the same widget doesn't work.delete
function that clears the grid of the box that you input. I defined it in the class's __init__
.command = ...
should be a function. Your original code attempted to make a function call in the command = ...
bit. This is a common mistake and leads to the function being executed when you create the button and then never again. I used a bound method because bound methods get self
passed implicitly.Upvotes: 1