Tkinter新创建的按钮不执行命令

大家好,
脚本应该打开一个应用程序,其中有两个按钮可见。当按下Hello按钮时,1号行中将显示一个新按钮,并且Hello按钮将被停用。当这个新按钮被按下时,它应该会从网格中删除自己,并重新激活Hello按钮,但它没有做到这一点。有什么主意吗?
请检查
视频
才能看到它的实际作用。

选择 | 换行 | 行号
  1. from tkinter import *
  2.  
  3. class Application(Frame):
  4.     def __init__(self, master=None):
  5.         self.master = master
  6.         self.master.geometry('300x100+10+10')
  7.         Frame.__init__(self, master)
  8.         self.pack()
  9.         self.createWidgets()
  10.  
  11.     def new_button(self):
  12.         print("enable_b")
  13.         self.hi_there.config(state=ACTIVE)
  14.         self.new_button.grid_remove()
  15.  
  16.     def say_hi(self):
  17.         print("hi there, everyone!")
  18.         self.new_button = Button(self)
  19.         self.new_button["text"] = "New BTN"
  20.         self.new_button.grid(row=1,column=0)
  21.         self.hi_there.config(state=DISABLED, command=self.new_button)
  22.  
  23.     def createWidgets(self):
  24.         self.QUIT = Button(self)
  25.         self.QUIT.config(text="QUIT",fg="red",command=self.quit)
  26.         self.QUIT.grid(row=0,column=1)
  27.         self.hi_there = Button(self)
  28.         self.hi_there["text"] = "Hello",
  29.         self.hi_there["command"] = self.say_hi
  30.         self.hi_there.grid(row=0,column=0)
  31.  
  32.     def quit(self):
  33.         self.master.destroy()
  34.  
  35. def testit(): 
  36.     root = Tk()
  37.     app = Application(master=root)
  38.     app.mainloop()
  39.  
  40. if __name__ == '__main__':
  41.     testit()
# 回答1


没有人会点击公共论坛上未知的(视频)链接。根据以下代码,您可以使用2个函数,每个按钮一个,也可以使用一个变量和一个函数。

选择 | 换行 | 行号
  1. from tkinter import *
  2.  
  3.     class Application():
  4.         def __init__(self, master=None):
  5.             self.master = master
  6.             self.master.geometry('300x100+10+10')
  7.             self.buttons_state=True  ## True= hi_there, False=new_buttpn
  8.             self.createWidgets()
  9.  
  10.         def change_buttons(self):
  11.             if self.buttons_state:
  12.                 self.hi_there.config(state=ACTIVE)
  13.                 self.new_button.grid_remove()
  14.             else:
  15.                 self.hi_there.config(state=DISABLED)
  16.                 self.new_button.grid(row=1,column=0)
  17.  
  18.             self.buttons_state = not self.buttons_state
  19.  
  20.         def createWidgets(self):
  21.             self.QUIT = Button(self.master)
  22.             self.QUIT.config(text="QUIT",fg="red",
  23.                              command=self.master.quit)
  24.             self.QUIT.grid(row=9,column=0)
  25.  
  26.             self.hi_there = Button(self.master, state=DISABLED)
  27.             self.hi_there["text"] = "Hello"
  28.             self.hi_there["command"] = self.change_buttons
  29.             self.hi_there.grid(row=0,column=0)
  30.  
  31.             self.new_button = Button(self.master, command=self.change_buttons)
  32.             self.new_button["text"] = "New BTN"
  33.             self.new_button.grid(row=1,column=0)
  34.             self.master.rowconfigure(1, weight=1, minsize=20)
  35.  
  36.     if __name__ == '__main__':
  37.         root = Tk()
  38.         app = Application(master=root)
  39.         root.mainloop()  

标签: python

添加新评论