Python 2.7,多处理和Tkinter

时间:2014-12-01 16:50:09

标签: python tkinter multiprocessing

我正在尝试使用多处理创建一个从python的新实例启动tkinter表单的类。

我的问题是:将Tkinter类对象传递给多处理函数的推荐方法是什么?当我通常将对象传递给多处理池时,我必须将对象转换为字符串。这是否意味着我必须挑选课程?有更好的方法吗?

我不能使用线程,因为我必须使用的一些python包不是线程安全的.. :(

示例代码:

import Tkinter as Tk

########################################################################
class TkinterApp(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""

        self.root = parent
        self.root.title("Main frame")
        self.frame = Tk.Frame(parent)
        self.frame.pack()

        btn = Tk.Button(self.frame, text="Close Frame", command=self.hide)
        btn.pack()

    #----------------------------------------------------------------------
    def hide(self):
        """"""
        self.root.withdraw()

系统规格:

  • Windows 7
  • Python 2.7.5 32位

非常欢迎任何建议或反馈!

谢谢

1 个答案:

答案 0 :(得分:0)

我还没有遇到任何问题,在池外创建Tkinter实例,以及类实例,然后从单独的进程调用函数。 A"简单"例子在下面

from multiprocessing import Process
import time

try:
    import Tkinter as tk    ## Python 2.x
except:
    import tkinter as tk    ## Python 3.x

class ProgressBar():
    def __init__(self, root):
        self.root=root
        self.root.geometry("75x50+900+100")
        self.ctr=25

    def mainloop(self):
        self.root.mainloop()

    def start_countdown(self):
        """ a separate process in a separate GUI
        """
        self.root.withdraw()
        self.top_count=tk.Toplevel(self.root)
        self.top_count.geometry("75x50+750+50")
        self.label_ctr = tk.IntVar()
        self.label_ctr.set(self.ctr)
        label = tk.Label(self.top_count, textvariable=self.label_ctr)
        label.pack()
        if self.ctr > 0:
            self.top_count.after(750, self.update)

    def start_running(self):
        """ create the progress bar widget
        """
        self.top=tk.Toplevel(self.root, takefocus=True)
        self.top.title("Progress Bar")
        self.top.geometry("+700+200")
        canvas = tk.Canvas(self.top, width=261, height=60, bg='lightgray')
        canvas.pack()

        rc2 = canvas.create_rectangle(15, 20, 243, 50, outline='blue', \
                                      fill='lightblue')
        rc1 = canvas.create_rectangle(24, 20, 34, 50, outline='white', \
                                      fill='blue')

        total=100
        x = 5
        while self.ctr:        ## move the small rectangle +5 or -5 units
            total += x
            if total > 311:
                x = -5
            elif total < 100:
                x = 5
            time.sleep(0.2)
            canvas.move(rc1, x, 0)
            canvas.update()

    def update(self):
        self.ctr -= 1
        self.label_ctr.set(self.ctr)

        if self.ctr > 0:
            self.top_count.after(750, self.update)
        else:
            ## sleep to allow any remaining after() to execute
            ## can also use self.root.after_cancel(id)
            self.top_count.after(500, self.root.destroy) ## destroy root when zero is reached

root = tk.Tk()

PB=ProgressBar(root)
pr1=Process(target=PB.start_countdown(), args=())
pr1.start()

pr2=Process(target=PB.start_running(), args=())
pr2.start()

## start mainloop in a separate process as a function of the class
## don't know if this is really necessary or not
## the theory is, it is detached from the other 2 processes and so
##    can react to both independently
## also the mainloop() process can be killed=shut down properly
pr3=Process(target=PB.mainloop(), args=())
pr3.start()

## safety clean up
pr1.terminate()
pr2.terminate()
pr3.terminate()