473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Tkinter newly created button does not execute command

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
1 1377
dwblas
626 Expert 512MB
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

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

Similar topics

2
by: yootzee | last post by:
Greetings all, I'm having a problem with scrolling vertically in a newly created browser window. The vertical scrolling is to reach some anchor point within the new browser window. I'm...
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
0
by: Earl Teigrob | last post by:
I am having a problem where the bitmap image that I just created and saved to disk can not be found but the code in another area of the program. When I reattempt to access the same image, it...
4
by: mcwooq | last post by:
Hi, I just installed the VS.Studio 2005 Team Edition for Developper and encountered severe problems with debugging ASP.NET 2.0 projects. Even newly empty created ASP 2.0 projects can't debug...
1
by: mcmwhite | last post by:
Hi, Does anyone know how I can open a newly-created HTML page in a new window, using ASP.NET? Eg. I have an ASP.NET page which creates some new HTML, based on what the user has entered into a...
2
by: sankaba | last post by:
Hi, I have created a new user for a database that i hav created in DB2, by expanding the database tree diagram in control center right upto DB users folder, and then right clicking it and...
2
by: Craig Taylor | last post by:
I have a javascript function as follows: (without the prefixed *'s) function addTest( ) { ePop = document.getElementById( "popups" ); eWin2 = document.createElement( "div");...
0
by: Chris B | last post by:
Howdy, When the AddNew button is clicked on the BindingNavigator, a new row is made in the datagrid and the row selector moves to that newly created row, what is the method that moves the selector...
22
by: DL | last post by:
Hi, What I wanted to do is to call a function from a newly created element. But it stumbled me. Here's the line that references the newly created element and I used the alert function for...
1
by: Paladius | last post by:
Hello, Within my Access program I create a new database using VBA. After the new database is created, I would like to set a form in it to appear on startup. I have tried using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.