dragon123
dragon123

Reputation: 303

python tkinter- more than one windows to show and do different processes

I have a GUI application. It has more than one windows. Main windows is updating every second with new values. I am using threading and queue here. There is a button in the main window. Pressing it will open a new window. What I want is, when new window is shown, stop the process in the main window (updating the values process) and some new process will be shown in the new window. Closing the new window should revoke the main window again and continue the process. Do i need to use multi threading or multi processing? Unfortunately, I am asking this question for the second time. sorry for that.

import tkinter
import time
import threading
import random
import queue

class GuiPart:
    def __init__(self, master, queue, endCommand,newWindow):
        self.queue = queue
        self.pause = False
        # Set up the GUI
        console = tkinter.Button(master, text='Done', command=endCommand)
        console.pack()

        console2 = tkinter.Button(master, text='New', command=newWindow)
        console2.pack()

        self.output = tkinter.StringVar()
        #output.set(1)
        output_1_label = tkinter.Label(master, textvariable= self.output, height=2, width=12)
        output_1_label.pack()
        # Add more GUI stuff here
        self.temp_process()

    def temp_process(self):
        if not self.pause:
            print ("handling messages")
        else:
            print ("Not handling messages")
        root.after(1000,self.temp_process)


    def processIncoming(self):
        while self.queue.qsize():
            try:
                msg = self.queue.get(0)
                print (msg)
                self.output.set(msg)
            except queue.Empty:
                pass

class ThreadedClient:

    def __init__(self, master):
        self.master = master

        # Create the queue
        self.queue = queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self.endApplication,self.create_window)

        self.running = 1
        self.thread1 = threading.Thread(target=self.workerThread1)  #this is for sending data to queue.
# what about second window?
        self.thread1.start()
        self.periodicCall()

    def on_quit(self):
        self.gui.pause = False
        self.window.destroy()

    def create_window(self):
        self.window = tkinter.Toplevel(root)
        self.gui.pause = True
        self.window.protocol("WM_DELETE_WINDOW",self.on_quit)

    def periodicCall(self):
        self.gui.processIncoming()
        if not self.running:
            import sys
            sys.exit(1)
        self.master.after(1000, self.periodicCall)

    def workerThread1(self):
        while self.running:
            time.sleep(rand.random() * 1)
            msg = rand.random()
            self.queue.put(msg)

    def endApplication(self):
        self.running = 0
rand = random.Random()
root = tkinter.Tk()
client = ThreadedClient(root)
root.mainloop()

Upvotes: 0

Views: 597

Answers (1)

Henry Yik
Henry Yik

Reputation: 22503

You can have your function processIncoming check for a flag, and pause/resume when required.

class GuiPart:
    def __init__(self, master, queue, endCommand,newWindow):
        self.queue = queue
        self.pause = False
        ....

    def processIncoming(self):
        while self.queue.qsize() and not self.pause:
            try:
                msg = self.queue.get(0)
                print (msg)
                self.output.set(msg)
            except queue.Empty:
                pass

Upvotes: 1

Related Questions