问题描述
限时送ChatGPT账号..每当我点击重启按钮更新notification1(标签)的文本
whenever I click the restart button to update the text of notification1 (label)
from tkinter import *
import tkinter as tk
from multiprocessing import Process
def notify(text):
notify_heading_text.set(f"App : {text}")
def on_restart():
notify("This text will be updated")
t1 = Process(target=print_hi)
t1.daemon = True
t1.start()
def print_hi():
notify("Hi There")
if __name__ == '__main__':
root = Tk()
root.geometry('400x400')
root.resizable(0, 0)
global notify_heading_text
notify_heading_text = tk.StringVar()
notification1 = Label(height=1, textvariable=notify_heading_text, bd=0)
notification1.pack(fill=X, side=TOP)
notification1.configure(bg='white', fg='black', font=('Helvetica', 12, 'bold'), pady=10)
bind3 = tk.StringVar()
b3 = Button(root, borderwidth=0, fg="white", activebackground='#40556a', activeforeground='white', bg="black",
textvariable=bind3, command=on_restart, font=('Helvetica', 12, 'bold'), padx=10, pady=6)
bind3.set('Restart')
b3.pack(side=RIGHT)
root.mainloop()
我收到此错误
Process Process-1:
Traceback (most recent call last):
File "C:\Users\prana\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Users\prana\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\prana\PycharmProjects\tkinter\main.py", line 18, in print_hi
notify("Hi There")
File "C:\Users\prana\PycharmProjects\tkinter\main.py", line 7, in notify
notify_heading_text.set(f"App : {text}")
NameError: name 'notify_heading_text' is not defined
notify("This text will be updated")
在 on_restart() 方法中工作正常并更新文本,但在 print_hi() 方法不更新 Tkinter 窗口的文本说 'notify_heading_text' 没有定义,尽管它被声明为一个全局变量.出于某种原因,我无法使用 root.after()
更新 Tkinter 窗口的显示而不会出错并且工作正常,但我想线程.
notify("This text will be updated")
inside on_restart() method works fine and updates text but that same method inside print_hi() method does not update the text of Tkinter window
says 'notify_heading_text' is not defined although it is declared as a global variable. For some reason, I can't use root.after()
which updates the display of the Tkinter window without errors and works fine but I want to thread.
推荐答案
试试这个:
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()
您的代码的问题在于您正在使用 multiprocessing
并且使用自己的内存创建了一个新的 python 进程.新的 python 进程找不到全局 notify_heading_text
变量,所以它会抛出错误.我将 multiprocessing
切换到 threading
,现在它应该可以工作了.一个进程内的不同线程使用相同的内存,这就是为什么不再出现错误的原因.
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.
也只是为了简化代码,我替换了 tk.StringVar
并使用了 <tkinter.Label>.config(text=<new text>)
Also just to simplify the code I replaced the tk.StringVar
s and used <tkinter.Label>.config(text=<new text>)
我在 IDLE 中看不到错误消息的原因是错误已写入控制台,但在 IDLE 情况下,它在没有控制台的情况下运行.因此错误仍然存在,但无法看到它.同时阅读这证明multiprocessing
找不到您正在引用的全局变量.这就是我大部分时间使用 threading
的原因.
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.
这篇关于无法在 tkinter python 中更新文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论