Hi all,
I am getting pretty far with my GUI but I have one major issue I cannot solve. I stripped down my program to the absolute bare minimum of my issue. I think the issue is that I cannot over write my tkinter label using a thread.
The code fully runs. Just press "s" on your keyboard to start the thread.
Upon opening the script, my tkinter Label correctly shows "initial words". Then I press "s" to start the thread, this prints the words "one" and "two" and calls the function changeState. Even though it prints correctly, changeState does not do it's job (to change the label text to "updated words"). I have no idea why!!
I am getting pretty far with my GUI but I have one major issue I cannot solve. I stripped down my program to the absolute bare minimum of my issue. I think the issue is that I cannot over write my tkinter label using a thread.
The code fully runs. Just press "s" on your keyboard to start the thread.
Upon opening the script, my tkinter Label correctly shows "initial words". Then I press "s" to start the thread, this prints the words "one" and "two" and calls the function changeState. Even though it prints correctly, changeState does not do it's job (to change the label text to "updated words"). I have no idea why!!
import tkinter as tk
import time
from threading import Thread
import keyboard
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):
self.state_lbl['text'] = "updated words" # this line is not working!
class MyFunctions:
def one(self):
print("one") # this line works fine
main_instance = MainPage() # is this wrong?
main_instance.changeState() # is this wrong?
time.sleep(1)
def two(self):
print("two") # this line works fine
time.sleep(1)
class runCycle(Thread):
def __init__(self):
Thread.__init__(self)
my = MyFunctions()
self.twoFunctions = [my.one, my.two]
def run(self):
for func in self.twoFunctions:
func()
run_instance = runCycle()
keyboard.add_hotkey('s', callback=run_instance.run) # keyboard "S" used to start program
if __name__ == "__main__":
root = tk.Tk()
main = MainPage(root)
main.grid()
root.wm_geometry("600x300")
root.mainloop()
