By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,254 Members | 2,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,254 IT Pros & Developers. It's quick & easy.

Tkinter newly created button does not execute command

P: 1
Hello all,

A script should open an application with two buttons visible. When Hello button is pressed a new button is gridded into the row number 1 and Hello button to be deactivated. When this new button is pressed it should delete itself off the grid and reactivate hello button but it does not do it. Any ideas?

Please check the video to see it in action.

Expand|Select|Wrap|Line Numbers
  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()
Jul 20 '18 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 621
No one is going to click an unknown (video) link from a public forum. You can use 2 functions, one for each button, or you can use a variable and one function, per the following code.
Expand|Select|Wrap|Line Numbers
  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()  
Jul 21 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.