Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 要更新到正确网格位置的多个进程_Python_Tkinter_Multiprocessing - Fatal编程技术网

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

Python 要更新到正确网格位置的多个进程,python,tkinter,multiprocessing,Python,Tkinter,Multiprocessing,我试图创建三个进程,每个进程最终都会从文本文件中提取,并进行更复杂的计算,但目前我只是在试验tkinter和多处理。我希望三个标签(lbl1、lbl2、lbl3)中的每一个都向其标签添加1、2或3并返回该值。我对管道和事件不熟悉,不了解如何将值返回到网格,并同步这些值,以便它们返回到正确的位置,因此根据网格中的位置,输出是随机的。如何使我返回的内容只返回到我已经指定的网格中的正确位置 import matplotlib.pyplot as plt import socket, time, da

我试图创建三个进程,每个进程最终都会从文本文件中提取,并进行更复杂的计算,但目前我只是在试验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()
问题:。。。我返回的任何东西都只返回到正确的位置

概括您的
def计数(…
),为此添加一个
位置
参数。 例如:

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
问题:…我返回的任何内容都只会返回到正确的位置

概括您的
def计数(…
),为此添加一个
位置
参数。 例如:

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

每个进程都需要将自己的管道发送到父进程,或者您需要在数据中跨管道发送一个进程标识符/索引,以确定数据来自哪个进程。正如在更新函数中所述,您无法保证将进程数据放置在管道中的顺序。请尝试更改self.parent\u管道、self.child\u管道=Pipe()到三个命令:parent\u Pipe,child\u Pipe=Pipe()self.parent\u Pipe.append(parent\u Pipe)self.child\u Pipe.append(child\u Pipe)至少这是解决问题的一种方法。每个进程都需要将自己的管道发送给父进程,或者您需要通过管道在数据中发送一个进程标识符/索引,以确定数据来自哪个进程。正如更新函数中所述,您无法保证流程数据在管道中的放置顺序。请尝试c将self.parent\u pipe、self.child\u pipe=pipe()挂起到三个命令:parent\u pipe、child\u pipe=pipe()self.parent\u pipe.append(parent\u pipe)self.child\u pipe.append(child\u pipe)至少这是解决问题的一种方法。我不认为管道可以返回两个值。尝试它或离开它。我没有解决问题,但让我以正确的方式思考,谢谢。我不认为管道可以返回两个值。尝试它或离开它。我没有解决问题,但让我以正确的方式思考,谢谢