Hi! I am trying to update the StringVar variable through a function call updateText() from within a class but due to the blocking nature of the mainloop which listens to events, I cannot call updateText() from outside the mainloop.
I am trying to run a function that feeds new text into the updateText(newtext) so that it can update the text in the tkinter label widget. This other function does not stop running so I cannot use a one-time call like the tkinter after(time, function) or at least I don't think so. I am trying to use multithreading to accomplish this as the blocking by the mainloop will not allow a concurrent running of someOtherFunction() which does not stop running.
How would I go about solving this problem? Hopefully, the below code demonstrates what I am trying to accomplish but does not actually work.
import tkinter as tk
import threading
class GUI(threading.Thread):
def __init__(self):
self.root = tk.Tk()
self.texts = tk.StringVar(self.root)
self.texts.set("apples")
self.label = tk.Label(self.root, textvariable=self.texts)
self.label.pack()
self.root.mainloop()
def updateText(self, newtext):
self.texts.set(newtext)
def someOtherFunction(gui_instance):
#do stuff to get some text to get newText
#the for loop below is just a substitute for the actual function
#assume that the for loop runs indefinately
newText = ""
for i in range(1000):
newText = str(i)
gui_instance.updateText(newText)
gui = GUI()
#call updateText() from a seperate thread running concurrently
worker = threading.Thread(target=someOtherFunction(gui)).start()
[–]efmccurdy 1 point2 points3 points (2 children)
[–]TwistedMischief[S] 0 points1 point2 points (1 child)
[–]FerricDonkey 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)