Tkinter新创建的按钮不执行命令
大家好,
脚本应该打开一个应用程序,其中有两个按钮可见。当按下Hello按钮时,1号行中将显示一个新按钮,并且Hello按钮将被停用。当这个新按钮被按下时,它应该会从网格中删除自己,并重新激活Hello按钮,但它没有做到这一点。有什么主意吗?
请检查
视频
才能看到它的实际作用。
选择 | 换行 | 行号
- from tkinter import *
- class Application(Frame):
- def __init__(self, master=None):
- self.master = master
- self.master.geometry('300x100+10+10')
- Frame.__init__(self, master)
- self.pack()
- self.createWidgets()
- def new_button(self):
- print("enable_b")
- self.hi_there.config(state=ACTIVE)
- self.new_button.grid_remove()
- def say_hi(self):
- print("hi there, everyone!")
- self.new_button = Button(self)
- self.new_button["text"] = "New BTN"
- self.new_button.grid(row=1,column=0)
- self.hi_there.config(state=DISABLED, command=self.new_button)
- def createWidgets(self):
- self.QUIT = Button(self)
- self.QUIT.config(text="QUIT",fg="red",command=self.quit)
- self.QUIT.grid(row=0,column=1)
- self.hi_there = Button(self)
- self.hi_there["text"] = "Hello",
- self.hi_there["command"] = self.say_hi
- self.hi_there.grid(row=0,column=0)
- def quit(self):
- self.master.destroy()
- def testit():
- root = Tk()
- app = Application(master=root)
- app.mainloop()
- if __name__ == '__main__':
- testit()