954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Updating a Tkinter Widget:

My current GUI program, in part, will display a button which the user may choose to press (or not, in some cases):

btn1 = Button(root ,bg="Black", text="HIT", image=image01,command=show_image2)
btn1.configure(state=NORMAL,background='red' )
btn1.pack()


This is basic and works fine.

The issue I am having is that after the button is chosen and pressed I would like this button to becomedisabled; this would require an update of sorts. Logically, as far as I can tell, this would be to use update_idletasks(). I have used this in other areas of the code with animation (the display of GIFs, etc) and it works most excellent. I have tried it along with the above code but it does not disable the button as I wish it to:

btn1.update_idletasks()
btn1 = Button(root ,bg="Black", text="HIT",   image=image01,command=show_image2)
btn1.configure(state=DISABLE,background='red' )
btn1.pack()


The syntax is incorrect and\or I am missing something that should be in there. I have the suspicion that there is more to updating a widget (a button) than there is a GIF but no research or documentation has shown this.

Thank-you for all help in advance.

sharky_machine

mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
 

Hi!

I don't have to update anything when I disable a button:

from Tkinter import *

def disable_it():
    b.configure(text="Disabled", state=DISABLED)

root = Tk()

b = Button(text="Enabled", command=disable_it)
b.pack()

root.mainloop()

I haven't tested it with gif's, maybe there is a difference.

Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

What do you do to re-enable the button?

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Hi! I don't have to update anything when I disable a button:

from Tkinter import *
 
def disable_it():
    b.configure(text="Disabled", state=DISABLED)
 
root = Tk()
 
b = Button(text="Enabled", command=disable_it)
b.pack()
 
root.mainloop()

I haven't tested it with gif's, maybe there is a difference. Regards, mawe



Thank-you for your reply. This is a nice, tidy solution. The problem is:command=disable_it. I already have a command loaded into my button code for this particular button (when the user chooses and presses this button a card image is displayed in the GUI). I cannot with Python\ Tkinter, unfortunately, have more than one (1) command in the code. I need to figure out another way to make this same thing happen minus "command". I like your submitted example and I am going to save it in my code snippet library as it seems a nice way to deal with button display issues in the future. I learned something useful today. Thank-you.

Regards,
sharky_machine

mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
 

You could simply add something like line
btn1.configure(state=DISABLED)
into your show_image2 function.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
What do you do to re-enable the button?


Use something like b.configure(text="Enabled", state=NORMAL)

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
from Tkinter import *

def disable_it():
    b.configure(text="Disabled", state=DISABLED)
    do_something_else()

def do_something_else():
    print "something_else"

def enable_it():
    b.configure(text="Enabled", state=NORMAL)

root = Tk()

b = Button(text="Enabled", command=disable_it)
b.pack()
Button(text="ReEnable", command=enable_it).pack()

root.mainloop()
mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You