我正在尝试创建一个简单的绘图程序,其中的按钮通过删除其上的所有形状来清除画布,但是我尝试使用内置到tkinter中的delete()命令,但是当我尝试运行它时说删除没有定义。我做错了什么?
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()
这是完整的追溯@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
答案 0 :(得分:1)
以下是一些可以帮助您入门的工作代码:
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()
我尽可能地保持原始代码的真实性。
要注意的一些事情......
.pack
按钮,因为在同一个小部件中使用.grid
和.pack
不起作用。delete
函数,用于清除您输入的框的网格。我在班级__init__
中定义了它。command = ...
应该是一个功能。您的原始代码尝试在command = ...
位中进行函数调用。这是一个常见的错误,导致在创建按钮时执行该功能,然后再也不会执行。我使用了绑定方法,因为绑定方法会隐式传递self
。