-1

I want to update my tkinter window label with the new variable from the data outside the thread.

Here is my sample code:

class App(threading.Thread):
global small
global medium
global large
global jumbo
global reject
def __init__(self):
    threading.Thread.__init__(self)
    self.start()

def callback(self):
    self.root.quit()

def run(self):
    self.root = tk.Tk()
    self.root.protocol("WM_DELETE_WINDOW", self.callback)
    string1 = "SMALL: " + str(small) + '\n' + "MEDIUM: " + str(medium) + '\n' + "LARGE: " \
              + str(large) + '\n' + "JUMBO: " + str(jumbo) + '\n' + "REJECTED: " + str(reject) + '\n' + "TOTAL: " + str(total)
    label = tk.Label(self.root, text=string1)
    label.pack()


    self.root.mainloop()
app = App()
#actual code runs outside the App() thread

The variables there are being changed by the actual code running outside the thread. I tried using root.after() but I cannot make it work.

Titus Sutio Fanpula
  • 3,467
  • 4
  • 14
  • 33
  • ***"I tried using root.after()"***: Can't reproduce this, please make sure, code you posts actually behaves as you claim. Read [how to use after method?](https://stackoverflow.com/questions/25753632) – stovfl Feb 05 '20 at 11:51
  • The `label` is a local variable inside `run()` function that cannot be accessed outside the function. – acw1668 Feb 05 '20 at 13:01
  • The indentation in your example needs to be fixed. – Bryan Oakley Feb 05 '20 at 15:13

1 Answers1

-1

After the interpreter runs the line self.root.mainloop(), it will be stuck inside an infinite loop. The rest of the code after that line will not be run. So the code that changes all the variables must be before self.root.mainloop() or be triggered by buttons.

As to how to set button callbacks, you could refer to python Tk keeping label text after I set

AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38