如何从衍生进程(multiprocessing.process)更新Tkinter标签?

2024-09-30 22:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

摘要:在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()

如果我按下“简单”按钮,标签中的文本将更新。一切都好

但是: 如果我按“异步”按钮

  • 正如您可以通过我提供的打印验证的那样,异步过程开始
  • 执行标签文本更新行
  • 但是:我的问题是:GUI上的标签没有显示更新的文本

我想这样做的原因是: 因为我正在启动一个长时间运行的派生进程,之后,我想更新标签。但是,所有其他进程应并行运行。因此,我创建了一个函数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插入,但没有成功

请帮忙,谢谢


Tags: textfrom文本async进程tkinterdefroot
1条回答
网友
1楼 · 发布于 2024-09-30 22:21:57

您不太可能从另一个流程更新标签。这是可能的,但会非常复杂。相反,我建议您创建一个线程,在另一个进程中启动昂贵的代码,然后等待更新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()

相关问题 更多 >