I wrote a python script that does some stuff 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.
Problems have arisen while trying to display that output in a GUI (just as a basic label) using tkinter.
I can get the label to display the string for the first time... but it never updates.
This is really the first time I have tried to use tkinter, so it's likely I'm making a foolish error. What I've got looks logical to me, but I'm evidently going wrong somewhere!
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
#do some stuff that generates string as variable 'result'
outputText = result
#do some more stuff that generates new string as variable 'result'
outputText = result
#do some more stuff that generates new string as variable 'result'
outputText = result
if counter == 5:
break
root.mainloop()
I also tried:
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
#do some stuff that generates string as variable 'result'
outputText = result
var.set(outputText)
#do some more stuff that generates new string as variable 'result'
outputText = result
var.set(outputText)
#do some more stuff that generates new string as variable 'result'
outputText = result
var.set(outputText)
if counter == 5:
break
root.mainloop()
In both cases, 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.
It also seems possible I am meant to be using trace and callback somehow to make all this work...but I can't get my head around how that works (I tried, but didn't manage to make anything that even looked like it would do something, let alone actually worked).
I'm still very new to both python and especially tkinter, so, any help much appreciated but please spell it out for me if you can :)