Jun-06-2020, 08:33 AM
The thread should not be called with the method run, it should be called with the method start.
Inside the thread you have created a new instance of MainPage, the thread will need passing a reference to MainPage.
When calling the GUI from a thread you need to use the method after or the GUI will lockup.
I removed the use of keyboard as I don't have it installed.
Inside the thread you have created a new instance of MainPage, the thread will need passing a reference to MainPage.
When calling the GUI from a thread you need to use the method after or the GUI will lockup.
I removed the use of keyboard as I don't have it installed.
import tkinter as tk import time from threading import Thread class MainPage(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(**kwargs) self.state_lbl = tk.Label(self, text="initial words") self.state_lbl.grid(row=0, column=0) def changeState(self, text): self.state_lbl['text'] = text class MyFunctions: def __init__(self, gui): self.gui = gui def one(self): time.sleep(5) print("one") self.gui.after(0, self.gui.changeState("updated words")) time.sleep(1) def two(self): print("two") time.sleep(1) self.gui.after(0, self.gui.changeState("updated words again")) class runCycle(Thread): def __init__(self, gui): Thread.__init__(self) my = MyFunctions(gui) self.twoFunctions = [my.one, my.two] def run(self): for func in self.twoFunctions: func() if __name__ == "__main__": root = tk.Tk() main = MainPage(root) main.grid() root.wm_geometry("600x300") run_instance = runCycle(main) run_instance.start() root.mainloop()Take a look at the following [Tkinter] How to deal with code that blocks the mainloop, freezing the gui