摘要:在Python中,当我从衍生进程更新Tkinter标签文本时,GUI上的标签不会更新,尽管衍生进程已执行。如何使其从衍生进程更新
我正在使用Lubuntu 20.04中的Python 2.7.2
编辑:我也尝试了Python 3.8,不得不安装python3 tk extra,修改了一些语法(print命令后面的括号并用Tkinter替换Tkinter),但问题仍然存在结束编辑
以下是我的独立工作示例代码,供您试用:
from Tkinter import *
from multiprocessing import Process
# simple Label change invoked from "simple" button
def bnAction_sync():
print "bnAction_sync"
changeLabel()
print "bnAction_sync done"
# asynchronous label change invoked from Async button
def bnAction_async():
print "bnAction_async"
p = Process(target=changeLabel)
p.start()
print "bnAction_Async done"
def changeLabel():
print "change label"
lbl['text'] = "Text changed"
### Apr 19 2021:
### uncommenting the following line really updates label but makes program crash ###
# root.update_idletasks
print "change label done"
root = Tk()
btnSync = Button(root, text="simple", command=bnAction_sync)
btnSync.pack()
btnAsync = Button(root, text="async", command=bnAction_async)
btnAsync.pack()
lbl = Label(root, text="Initial text")
lbl.pack()
root.mainloop()
如果我按下“简单”按钮,标签中的文本将更新。一切都好
但是: 如果我按“异步”按钮
我想这样做的原因是: 因为我正在启动一个长时间运行的派生进程,之后,我想更新标签。但是,所有其他进程应并行运行。因此,我创建了一个函数f,它依次包含长时间运行的函数和标签更新函数。我想异步调用f。 因此,原则上:
def longprocess():
code...
def updatelabel_after_longprocess():
code...
def f():
longprocess()
updatelabel_after_longprocess()
p = Process(target=f)
p.start()
do other things immediately
在我读到的某个地方,刷新被暂停,而脚本仍在运行。 我尝试了一些p.join插入,但没有成功
请帮忙,谢谢
您不太可能从另一个流程更新标签。这是可能的,但会非常复杂。相反,我建议您创建一个线程,在另一个进程中启动昂贵的代码,然后等待更新GUI,直到该进程完成:
相关问题 更多 >
编程相关推荐