如何在Python中为Tkinter的grid_remove实现删除按钮功能。
我正在添加一个按钮,但如果我想要删除那个小部件的行和列,当我调用remove_material函数时,它却没有被删除。
def remove_material():
r=7
global r
#combo=Pmw.ComboBox(root,label_text='Select Material:',labelpos='w',scrolledlist_items=map(str, a)).grid(row = r, column = 2, sticky = 'w')
#Entry= Pmw.EntryField(root, labelpos = 'w',label_text = 'Thickness in mm:').grid(row = r, column = 4, sticky = 'w')
#lable=Tkinter.Label(root,text="mm.Outside").grid(row=r,column=5,sticky='w')
remove=Tkinter.Button(root,text="Remove",command='').grid(row=r,column=6,sticky='w')
r=r-1
#return combo,Entry,lable
#remove.grid(row=r,column=3)
#remove.grid_remove()
remove.grid_forget()
#remove.grid()
1 个回答
1
当你把一个小部件(widget)赋值给一个变量时,需要在另一行使用 grid
、pack
或 place
方法来放置它。因为这些方法是用来处理 tkinter 对象的。
remove=Tkinter.Button(root,text="Remove",command='')
remove.grid(row=r,column=6,sticky='w')
>>>type(remove)
>>><class 'Tkinter.Button'>
remove=Tkinter.Button(root,text="Remove",command='').grid(row=r,column=6,sticky='w')
>>>type(remove)
>>><class 'NoneType'>
所以这样应该是可以工作的。
remove=Tkinter.Button(root,text="Remove",command='')
remove.grid(row=r,column=6,sticky='w')
remove.grid_forget()
编辑:你的程序还会因为 global
声明而出现语法错误。你需要先把它定义为全局变量,然后再给它赋值。