在Linux上使用多处理时,不会出现TKinter窗口

时间:2009-01-04 04:36:35

标签: python linux tkinter multiprocessing

我希望生成另一个进程,以便在应用程序的其余部分继续时异步显示错误消息。

我正在使用Python 2.6中的multiprocessing模块来创建进程,我正在尝试使用TKinter显示窗口。

此代码在Windows上运行正常,但在Linux上运行它,如果我调用TKinter,则不会显示'showerror("MyApp Error", "Something bad happened.")'窗口。如果我通过直接调用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,它就会起作用,因为它是第一次导入它的子进程。

2 个答案:

答案 0 :(得分:4)

discussion可能会有所帮助。

  

以下是我发现的一些示例问题:

     
      
  1. 虽然多处理模块紧跟线程,但它绝对不是完全匹配。一个例子:从参数到a   进程必须是 pickleable ,我不得不经历很多代码   更改以避免传递Tkinter个对象,因为这些不是   与pickle 。线程模块不会发生这种情况。

  2.   第一次尝试后,
  3. process.terminate()无效。第二次或第三次尝试可能只是挂起翻译   因为数据结构已损坏(在API中提到,但是这个   很少安慰。)

  4.   

答案 1 :(得分:0)

在从同一个shell调用程序之前调用shell命令xhost +可能会有效吗?

我猜你的问题在于X服务器。

相关问题