Skip to content Skip to sidebar Skip to footer

Tkinter: Multiprocessing On Events That Happen Per Second

I have made a Tkinter program that reads from a modbus slave device. Every second it reads the device and displays the output onto labels. However, I have multiple tabs that run th

Solution 1:

The general idea is to put the code that reads from the sensors in a thread, and have that code communicate with the GUI thread via a queue.

Here's a real quick hack to demonstrate the technique:

import tkinter as tk
import threading
import queue
import random
import time

classExample(object):
    def__init__(self):
        self.root = tk.Tk()
        self.sensor_vars = []

        self.root.grid_columnconfigure(1, weight=1)
        for row, i inenumerate(range(3)):
            var = tk.StringVar()
            self.sensor_vars.append(var)
            label = tk.Label(self.root, text="Sensor %d:" % i)
            value = tk.Label(self.root, textvariable=var, width=4)
            label.grid(row=row, column=0, sticky="e")
            value.grid(row=row, column=1, sticky="w")

        # create a queue for communication
        self.queue = queue.Queue()

        # create some sensors
        self.sensors = []
        for i inrange(3):
            sensor = Sensor(self.queue, i)
            self.sensors.append(sensor)
            sensor.setName("Sensor %d" % i)

        # start polling the queue
        self.poll_queue()

    defstart(self):
        # start the sensorsfor sensor in self.sensors:
            sensor.start()

        # start the GUI loop
        self.root.mainloop()

        # wait for the threads to finishfor sensor in self.sensors:
            sensor.stop()
            sensor.join()

    defpoll_queue(self):
        ifnot self.queue.empty():
            message = self.queue.get()
            index = message["index"]
            self.sensor_vars[index].set(message["value"])

        self.root.after(100, self.poll_queue)

classSensor(threading.Thread):
    def__init__(self, queue, index):
        threading.Thread.__init__(self)
        self.queue = queue
        self.index = index
        self.stop_requested = Falsedefstop(self):
        self.stop_requested = Truedefrun(self):
        for i inrange(10):
            if self.stop_requested:
                break
            value = random.randint(10, 100)
            self.queue.put({"index": self.index, "value": value})
            time.sleep(1)



if __name__ == "__main__":
    app = Example()
    app.start()

Post a Comment for "Tkinter: Multiprocessing On Events That Happen Per Second"