首页 > 解决方案 > 如何从衍生进程 (multiprocessing.Process) 更新 Tkinter 标签?

问题描述

摘要:在 Python 中,当我从衍生进程更新 Tkinter 标签文本时,尽管执行了衍生进程,但 GUI 上的标签并未更新。我怎样才能让它从生成的进程中更新?

我正在 Lubuntu 20.04 中使用 Python 2.7.2

编辑我也尝试使用 Python 3.8,必须额外安装 python3-tk,更改一些语法(打印命令后的括号并将 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 插入,但没有运气。

请帮忙,谢谢!

标签: pythonpython-3.xpython-2.7python-multiprocessingtkinter-label

解决方案


您不太可能从另一个流程中更新您的标签。这可能是可能的,但它会非常复杂。相反,我建议您创建一个线程,在另一个进程中启动昂贵的代码,然后等待更新 GUI,直到该进程完成:

from multiprocessing import Process
from threading import Thread

def longprocess():
    # do whatever you need here, it can take a long time

def updatelabel_after_longprocess():
    # do whatever GUI stuff you need here, this will be run in the main process

def thread_helper():
    process = Process(target=longprocess)
    process.start()
    process.join() # this waits for the process to end
    updatelabel_after_longprocess()

if __name__ == "__main__":
    t = Thread(target=thread_helper)
    t.start()

    # do whatever else you have to do in parallel here

    t.join()

推荐阅读