无法从TKinter GUI使用多重处理模块执行多种功能

时间:2019-04-03 19:03:42

标签: python-3.x tkinter

嗨,我有一个小的GUI,它带有一个按钮,必须使用该按钮在不同的处理器中执行两个功能。实际上,这两个功能是繁重的计算。我不想使用多线程。我希望它们在2个不同的处理器上运行。当我尝试执行按钮时,将创建另一个GUI实例,并显示

File "C:\Python3.7\lib\multiprocessing\reduction.py", line 60, in dump. 
ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _tkinter.tkapp objects

我的代码如下。

from multiprocessing import Process
from tkinter import Button, Tk, Frame

class GUI(Frame):

    def __init__(self):
        super().__init__()
        self.button = Button(self, text="Start", command=self.execute)
        self.button.pack()
        self.pack()

    def F1(self):
        print("Hello")

    def F2(self):
        print("World")

    def execute(self):
        self.P1 = Process(target = self.F1)
        self.P2 = Process(target = self.F2)

        self.P1.start()
        self.P2.start()

        self.P1.join()
        self.P2.join()


Root = Tk()
Software = GUI()
Root.mainloop()

Please click here

1 个答案:

答案 0 :(得分:-1)

问题在于腌制tkinter小部件。您根本做不到,因为Tcl解释器无法理解python pickle格式。

来到您的代码中,我尝试了以下操作,并按预期进行打印:

from multiprocessing import Process
from tkinter import Button, Tk, Frame

class GUI(Frame):

    def __init__(self):
        super().__init__()
        self.button = Button(self, text="Start", command=self.execute)
        self.button.pack()
        self.pack()

    @staticmethod
    def F1():
        print("Hello")

    @staticmethod
    def F2():
        print("World")

    def execute(self):
        self.P1 = Process(target = GUI.F1())
        self.P2 = Process(target = GUI.F2())

        self.P1.start()
        self.P2.start()

        self.P1.join()
        self.P2.join()


Root = Tk()
Software = GUI()
Root.mainloop()