Tkinter中的线程和过程控制

时间:2019-08-05 13:31:12

标签: python tkinter

我正在开发一个python tkinter应用程序,以连续监视两个过程(测量一些传感器值)。我有两个开始按钮和两个停止按钮。控制每个过程。我只想显示测量结果。由于这是一个继续的过程,因此我正在使用线程。一次测量后,我的测量就停止了。任何人都可以建议我问题是什么。我在下面显示一个开始和停止按钮来代表一个过程。 (此处尝试显示一些随机值。)

import tkinter
import time
import threading
import random
import queue

class AppGUI:
    def __init__(self, master, queue, endApplicationCommand, start_process_1, stop_process_1, client):
        self.queue = queue
        self.client = client
        self.val_process_1= tkinter.StringVar()
        # Set up the GUI
        Process_1_Result = tkinter.Label(master, text = "P-1" )
        Process_1_Result.place(x=15, y=100)
        Process_1_Result_Values = tkinter.Label(master, textvariable = self.val_process_1)
        Process_1_Result_Values.place(x=200, y=100)
        self.start_1 = tkinter.Button(master, text='Start P-1', command=self.client.start_process_1)
        self.start_1.place(x=500, y=300)
        self.stop_1 = tkinter.Button(master, text='Stop P-1',state=tkinter.DISABLED, command=self.client.stop_process_1)
        self.stop_1.place(x=500, y=400)

    def Process_Data(self):
        """
        Handle all the messages currently in the queue (if any).
        """

        while self.queue.qsize():
            try:
                msg = self.queue.get(0)
                # Check contents of message and do what it says
                # As a test, we simply print it
                print (msg)
                #self.output.set(msg)
                self.val_process_1.set(msg)
            except queue.Empty:
                pass


class Clinet_Thread:

    def __init__(self, master):
        self.master = master
        # Create the queue
        self.queue = queue.Queue()
        # Set up the GUI part
        self.gui = AppGUI(master, self.queue, self.endApplication, self.start_process_1, self.stop_process_1, client=self)
        self.running = 1
        self.start_status_P1=0
        self.thread1 = threading.Thread(target=self.New_Thread)
        self.thread1.start()
# Start the periodic call in the GUI to check if the queue contains anything
        self.Periodic_Call()

    def Periodic_Call(self):
        #I guess problem is here?
        if self.running==1:
            self.gui.Process_Data()
        if self.running==0:
            import sys
            sys.exit(1)
        self.master.after(1000, self.Periodic_Call)
    def New_Thread(self):
        if self.start_status_P1 == 1:
            #time.sleep(1) is it okay to sleep?
            msg = rand.randrange(0,10,1)
            self.queue.put(msg)
            print("started_P1")
        elif  self.start_status_P1 == 0:
            #time.sleep(1)
            print("stopped_P1")

    def endApplication(self):
        self.running = 0
    def start_process_1(self):
        self.start_status_P1 = 1
        self.gui.start_1.config(state="disabled")
        self.gui.stop_1.config(state="normal")
    def stop_process_1(self):
        self.start_status_P1 = 0
        self.gui.start_1.config(state="normal")
        self.gui.stop_1.config(state="disabled")

rand = random.Random()
root = tkinter.Tk()
client = Clinet_Thread(root)
root.mainloop()

0 个答案:

没有答案