I wrote a Python script that does some task to generate, and then keep changing some text stored as a string variable. This works, and I can print the string each time it gets changed.
I can get the Label to display the string for the first time, but it never updates.
Here’s my code:
from tkinter import *
outputText = 'Ready'
counter = int(0)
root = Tk()
root.maxsize(400, 400)
var = StringVar()
l = Label(root, textvariable=var, anchor=NW, justify=LEFT, wraplength=398)
l.pack()
var.set(outputText)
while True:
counter = counter + 1
outputText = result
outputText = result
outputText = result
if counter == 5:
break
root.mainloop()
The Label will show Ready, but won’t update to change that to the strings as they’re generated later.
After a fair bit of googling and looking through answers on this site, I thought the solution might be to use update_idletasks. I tried putting that in after each time the variable was changed, but it didn’t help.
The window is only displayed once the mainloop is entered. So you won’t see any changes you make in your
while Trueblock preceding the lineroot.mainloop().GUI interfaces work by reacting to events while in the mainloop. Here’s an example where the StringVar is also connected to an Entry widget. When you change the text in the Entry widget it automatically changes in the Label.
I like the following reference: tkinter 8.5 reference: a GUI for Python
Here is a working example of what you were trying to do:
root.updateEnter event loop until all pending events have been processed byTcl.