[Tutor] Tkinter and threading
Marco Soldavini
magyar1886 at gmail.com
Thu Jun 2 09:40:44 EDT 2016
Hello,
probably this is a very naive question, but I've read some stuff on
Tkinter and its infinite loop.
Then about how can i bind actions to elements, for example buttons.
What if I want to run another loop beside the graphical interface in
the same python script?
For example a state machine with a var state which can have some
discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE)
and a text element on the gui that reports that state.
So a button cause a transition > the python loop (not the gui loop)
detects the transition and changes the state var value > the gui
refresh its value on screen.
I wrote some code to try it without threading but it does not give the
expected result as it seems the button update status action is already
triggered. I am missing some basic point here
from Tkinter import *
state = "STOPPED"
root = Tk()
myContainer1 = Frame(root)
myContainer1.pack()
label1=Label(myContainer1, text=state, font = "Helvetica 16 bold italic")
label1.pack()
def change(new_state):
label1.config(text=new_state)
button1 = Button(myContainer1, text="START")
button1.bind("<Button-1>", change("START"))
button1.pack()
button2 = Button(myContainer1)
button2["text"]= "STOP"
button2.pack()
root.mainloop()
Thanks
marco
More information about the Tutor
mailing list