在Linux上使用多处理时,不会出现TKinter窗口
-
03-07-2019 - |
题
我希望生成另一个进程,以便在应用程序的其余部分继续时异步显示错误消息。
我正在使用Python 2.6中的 multiprocessing
模块创建进程,我正在尝试使用 TKinter
显示窗口。
此代码在Windows上运行正常,但在Linux上运行它时,如果我调用'showerror(“MyApp Error”,“发生了一些不好的事情。”),则不会出现
TKinter
窗口。 )'代码>。如果我通过直接调用 showerrorprocess
在同一进程中运行它, 就会出现。鉴于此,似乎 TKinter
正常工作。我可以打印到控制台,并通过 multiprocessing
生成的进程执行其他操作,因此它似乎也在起作用。
他们似乎没有一起工作。我是否需要做一些特殊的事情来允许衍生的子进程创建窗口?
from multiprocessing import Process
from Tkinter import Tk, Text, END, BOTH, DISABLED
import sys
import traceback
def showerrorprocess(title,text):
"""Pop up a window with the given title and text. The
text will be selectable (so you can copy it to the
clipboard) but not editable. Returns when the
window is closed."""
root = Tk()
root.title(title)
text_box = Text(root,width=80,height=15)
text_box.pack(fill=BOTH)
text_box.insert(END,text)
text_box.config(state=DISABLED)
def quit():
root.destroy()
root.quit()
root.protocol("WM_DELETE_WINDOW", quit)
root.mainloop()
def showerror(title,text):
"""Pop up a window with the given title and text. The
text will be selectable (so you can copy it to the
clipboard) but not editable. Runs asynchronously in
a new child process."""
process = Process(target=showerrorprocess,args=(title,text))
process.start()
修改强>
问题似乎是 TKinter
是由父进程导入的,而“继承的”是进入子进程,但不知何故,它的状态与父进程密不可分,并且它不能在子进程中起作用。只要您确保在生成子进程之前不导入 TKinter
,它就会起作用,因为它是第一次导入它的子进程。
解决方案
此讨论可能会有所帮助。
以下是我发现的一些示例问题:
虽然多处理模块紧跟线程,但它绝对不是完全匹配。一个例子:从参数到a 进程必须是 pickleable ,我不得不经历很多代码 更改以避免传递
Tkinter
对象,因为它们不是 与pickle 。线程模块不会发生这种情况。- 醇>
process.terminate()
在首次尝试后无法正常工作。第二次或第三次尝试可能只是挂起翻译 因为数据结构已损坏(在API中提到,但是这个 很少安慰。)
其他提示
在从同一个shell调用你的程序之前调用shell命令 xhost +
会有用吗?
我猜你的问题在于X服务器。