Multiprocessor communication - tkinter communication

I have a question about multiprocessing and tkinter. I am having some problems so that my process runs in parallel with the tkinter GUI. I created a simple practice example and read to understand the basics of multiprocessing. However, applying them to tkinter, only one process is running at this time. ( Using the Multiprocessing module to update the Tkinter GUI ) In addition, when I added a queue for communication between processes, ( How to use multiprocessing queue in Python? ), The process will not even start.

Purpose: I would like to have one process that counts and puts the values ​​in the queue, and one - update tkinter after 1 second and show me the values.

All tips are kindly appreciated.

Regards, S

EDIT : I want data to be available when the after method is called. Thus, the problem is not with the after function, but with the call to the after method. It will take 0.5 seconds to complete the calculation each time. Therefore, the GUI does not respond for half a second, every second.

EDIT2 : Corrections have been made to the code based on feedback, but this code has not yet been run.

class Countdown(): 
    """Countdown prior to changing the settings of the flows"""

    def __init__(self,q):

        self.master = Tk()
        self.label = Label(self.master, text="", width=10)
        self.label.pack()
        self.counting(q)

    # Countdown()
    def counting(self, q):
        try:
            self.i = q.get()
        except:
            self.label.after(1000, self.counting, q)

        if int(self.i) <= 0:
            print("Go")
            self.master.destroy()

        else:
            self.label.configure(text="%d" % self.i)
            print(i)
            self.label.after(1000, self.counting, q)

def printX(q):
    for i in range(10):
        print("test")
        q.put(9-i)
        time.sleep(1)
    return


if __name__ == '__main__':
    q = multiprocessing.Queue()

    n = multiprocessing.Process(name='Process2', target=printX, args = (q,)) 
    n.start() 

    GUI = Countdown(q)
    GUI.master.mainloop()
+4
source share
3 answers

Ipython. Python, iPython. spyder.

+2

mainloop , . mainloop .

+3

after. , .

self.label.after(1000, self.counting(q))

counting(q) , .

,

self.label.after(1000, self.counting, q)

Also, start your second process before creating the window and call counting.

n = multiprocessing.Process(name='Process2', target=printX, args = (q,)) 
n.start() 

GUI = Countdown(q)
GUI.master.mainloop()

Also you only need to call mainlooponce. In any position, you have work, but you just need


Edit: You also need to (9-i)queue to do the countdown.

q.put(9-i)

Inside function printX

+2
source

Source: https://habr.com/ru/post/1672535/


All Articles