Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Tkinter Button state from disabled to normal?

I need to change the state of a Button from DISABLED to NORMAL when some event occurs.

The button is currently created in the DISABLED state using the following code:

self.x = Button(self.dialog, text="Download", state=DISABLED,                 command=self.download).pack(side=LEFT) 

How can I change the state to NORMAL?

like image 829
scandalous Avatar asked Apr 16 '13 20:04

scandalous


People also ask

How do you change the state of a button in Python?

First, import the Tkinter package. Now we'll create an app object and set the window size to 200 x 200. We'll add two more buttons, button 1 and button 2. We'll offer an argument as an app that will be displayed in the app window, and we'll give it a name by setting text attributes to “Python Button 1.”

What are the states of the button recognized by Tkinter?

Tkinter Button has two states, NORMAL - The button could be clicked by the user. DISABLE - The button is not clickable.


1 Answers

You simply have to set the state of the your button self.x to normal:

self.x['state'] = 'normal' 

or

self.x.config(state="normal") 

This code would go in the callback for the event that will cause the Button to be enabled.


Also, the right code should be:

self.x = Button(self.dialog, text="Download", state=DISABLED, command=self.download) self.x.pack(side=LEFT) 

The method pack in Button(...).pack() returns None, and you are assigning it to self.x. You actually want to assign the return value of Button(...) to self.x, and then, in the following line, use self.x.pack().

like image 60
Sheng Avatar answered Oct 07 '22 14:10

Sheng