Not Able To Update Text In Tkinter Python
Solution 1:
Try this:
import tkinter as tk
from threading import Thread
def notify(text):
label.config(text=f"App : {text}")
def on_restart():
notify("This text will be updated")
t1 = Thread(target=print_hi)
t1.daemon = True
t1.start()
def print_hi():
notify("Hi There")
root = tk.Tk()
label = tk.Label(root, text="")
label.pack(fill="x", side="top")
button = tk.Button(root, text="Restart", command=on_restart)
button.pack()
root.mainloop()
The problem with your code is that you are using multiprocessing
and that creates a new python process with its own memory. The new python process can't find the global notify_heading_text
variable so it throws the error. I switched multiprocessing
to threading
and it should now work. Different threads inside a process use the same memory so that is why the error no longer appears.
Also just to simplify the code I replaced the tk.StringVar
s and used <tkinter.Label>.config(text=<new text>)
Edit:
The reason I couldn't see the error message in IDLE, is because the error is written to the console but in the IDLE case it runs without a console. Therefore the error was still there but there was no way of seeing it. Also reading this proves that multiprocessing
can't find the global variable that you are referencing. That is why I use threading
most of the time.
Post a Comment for "Not Able To Update Text In Tkinter Python"