473,588 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter newly created button does not execute command

1 New Member
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 1383
dwblas
626 Recognized Expert Contributor
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
5396
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 attempting to do this by using an anchor point name of an anchor located
29
2221
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> * f() { vector<int> * v = new vector<int>; return v; }
0
990
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 always works becuase the image has been fully saved to that drive by that time. Is there a way to make sure that a newly created bitmap image is phisically saved to disk before it is accessed? The code that creates that new bitmap image is as...
4
2409
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 (setting breakpoint in page_load()). I'm getting the following message: "Unable to start debugging on the web server. An error occurred that usually indicates a corrupt installation (code 0x80004002). If the problem persists,
1
1555
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 textbox. The ASP page has a textbox (txtYourName) and a button. Clicking the button will run this piece of code:
2
1548
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 selecting "Add...". It opens up a window, where we hav to specify the name of the user, and the various privileges to the new user. if we click"ok" the new user is created. BUT THE PROBLEM IS I DON'T KNOW WAT IS THE PASSWORD (or DEFAULT PASSWORD)...
2
3480
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"); eWin2.setAttribute( 'id', 'win2' ); eWin2.setAttribute( 'name', 'win2' ); eWin2.setAttribute( 'class', 'window' ); ** eWin2.style.backgroundColor = 'yellow';
0
853
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 to the new row? I have created a method that calls the AddNew method on a DefaultView and creates a new row. I need to move to that newly created row and get the value from a auto-incremented cell in the new row and then populate a combo box with...
22
1679
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 debugging for now. Did I mess up all these quotes? // once again this is the key line of code, problem area, disregard
1
1639
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 CurrentDb.Properties("StartupForm") but am not sure what I need to replace CurrentDB with in order to target the newly created DB. or Is their any way to export my "startup properties" of the current database to the newly created database?
0
7862
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.