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



我正在尝试创建三个过程,这些过程最终将从文本文件中删除并进行更复杂的计算,但是目前我只是在尝试TKINTER和多处理。我希望三个标签(LBL1,LBL2,LBL3(中的每个标签中的每个标签中的每个标签或3个标签加入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()

问题:...我返回的任何东西只返回正确的位置

概括您的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

相关内容

  • 没有找到相关文章

最新更新