多个进程更新到正确的网格位置

时间:2017-06-06 18:51:08

标签: python tkinter multiprocessing

我正在尝试创建三个进程,每个进程最终将从文本文件中提取并进行更复杂的计算,但目前我只是尝试使用tkinter和多处理。我希望三个标签(lbl1,lbl2,lbl3)中的每一个都在其标签上添加1,2或3并返回该值。我是管道和事件的新手,并且不了解将值返回到同步值的方式,以便它们返回到正确的位置,因此输出是根据网格中的位置随机的。如何才能使我返回的任何内容返回到我已指定的网格中的正确位置?

import matplotlib.pyplot as plt
import  socket, time, datetime, sys, struct
from datetime import datetime
import numpy as np
import shutil
import os
from random import randint
import Tkinter as tk
from multiprocessing import *
#from Tkinter import *

def count1(pipe,stop):
    x = 0
    while not stop.is_set():
        x+=1
        pipe.send(x)
        time.sleep(1)
def count2(pipe,stop):
    y = 0
    while not stop.is_set():
        y+=2
        pipe.send(y)
        time.sleep(1)
def count3(pipe,stop):
    z = 0
    while not stop.is_set():
        z+=3
        pipe.send(z)
        time.sleep(1)

class UpdatingGUI(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        self.parent = parent
        self.parent_pipe, self.child_pipe = Pipe()

        self.stop_event = Event()

        self.num1=tk.Label(self, text="Number 1")
        self.num1.grid(row=0, column=0)
        self.num2=tk.Label(self, text="Number 2")
        self.num2.grid(row=0, column=1)
        self.num3=tk.Label(self, text="Number 3")
        self.num3.grid(row=0, column=2)


        # label to show count value
        self.updating_int1 = tk.IntVar()
        self.updating_int1.set(0)
        self.updating_lbl1 = tk.Label(self,textvariable=self.updating_int1)
        self.updating_lbl1.grid(row=1, column=0)

        self.updating_int2 = tk.IntVar()
        self.updating_int2.set(0)
        self.updating_lbl2 = tk.Label(self,textvariable=self.updating_int2)
        self.updating_lbl2.grid(row=1, column=1)

        self.updating_int3 = tk.IntVar()
        self.updating_int3.set(0)
        self.updating_lbl3 = tk.Label(self,textvariable=self.updating_int3)
        self.updating_lbl3.grid(row=1, column=2)

        # launch count as a process
        self.counter1 = Process(target=count1,args=(self.child_pipe,self.stop_event))
        self.counter1.start()
        self.update()

        self.counter2 = Process(target=count2,args=(self.child_pipe,self.stop_event))
        self.counter2.start()
        self.update()

        self.counter3 = Process(target=count3,args=(self.child_pipe,self.stop_event))
        self.counter3.start()
        self.update()


    def update(self):
    # While the pipe has data, read and update the StringVar                                                                                
        while self.parent_pipe.poll():
            self.updating_int1.set(self.parent_pipe.recv())
            self.updating_int2.set(self.parent_pipe.recv())
            self.updating_int3.set(self.parent_pipe.recv())

        self.parent.after(1000,self.update)


def main():
    root = tk.Tk()
    root.geometry("400x200")
    gui = UpdatingGUI(root)
    gui.pack()    
    root.mainloop()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

  

问题:...我返回的任何内容只会返回正确的位置

概括您的def count (...,为此添加position参数。 例如:

def count(position, pipe, stop):
    x = 0
    while not stop.is_set():
        x += 1
        pipe.send((position, x))
        time.sleep(1)

def update(self):
    # While the pipe has data, read and update the StringVar                                                                                
    while self.parent_pipe.poll():
        position, data = self.parent_pipe.recv()
        if position == 1:
            self.updating_int1.set(data)
        elif position == 2:
            # and so on