I am trying to create a simple form using Tkinter that will do an action once a button is pressed. How do I "listen" for the button press?
I have created a callback method which will change a status variable when the button is pressed, but cannot figure out how to cause an action in my main loop once the button is pressed.
I have attempted to use a while loop (after the button is drawn) to check the value of the status variable, but when I do this, the loop executes, but my GUI elements don't appear on the screen. (If I use a for loop, they do appear, but I don't think that will work for this.)
How do I "wait" for a change in state to my "status" variable? Or is there a built-in way to do this that I am missing?
(The actual code is little more complicated - similar to the approach in the answer here (but without all the buttons being on one page) - but I think that the principle is still the same (how to listen for a change in state to one of the object variables).)
from Tkinter import *
master = Tk()
def callback():
status = 0
print status
status = 1
myB = Button(text="Enter", command=callback)
myB.pack()
print status
# while True:
# if status == 0:
# print "button was clicked"
mainloop()