Skip to content
Snippets Groups Projects
Commit 98e8fd53 authored by Leonardo Martinez Hornak's avatar Leonardo Martinez Hornak
Browse files

Thread added for communication between tasks

parent de646d72
Branches
Tags
No related merge requests found
#Reference for Threading and GUI architecture: https://maldus512.medium.com/how-to-setup-correctly-an-application-with-python-and-tkinter-107c6bc5a45
import queue
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
from tkinter import TOP, BOTH, X, LEFT, RIGHT
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from types import SimpleNamespace
from enum import Enum
from threading import Thread
TIME_OUT = 0.5 #Time out used for refreshing the User Interface
class Messages(Enum):
HRSETALARM = 0
#Method in charge of updating the UI. It is called from another thread and uses queue for communication
def refreshUI(guiRef, model, guiQueue):
msg = None
while True:
try:
msg = guiQueue.get(timeout = TIME_OUT)
if msg == Messages.HRSETALARM:
model.count += 1
guiRef.entry["state"] = 'normal'
guiRef.entry.delete(0,"end")
guiRef.entry.insert(0,"12")
guiRef.entry["state"] = 'disabled'
guiRef.entry_two.delete(0,"end")
guiRef.entry_two.insert(0,"Prueba")
except queue.Empty:
pass
#Other tasks...
print("Han pasado 500 milisegundos")
#Class for the User Interface
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
......@@ -83,10 +117,10 @@ class Application(tk.Frame):
self.label_units_heartrate_three = tk.Label(self.frame_four, text = "bpm")
self.label_units_heartrate_three.pack(side=LEFT)
self.button_heartrate = tk.Button(self.frame_four, text = "Configurar alarma", command = self.configure_heartrate_alarm)
self.button_heartrate = tk.Button(self.frame_four, text = "Configurar alarma")
#self.button_heartrate = tk.Button(self.frame_four, text = "Configurar alarma", command = self.configure_heartrate_alarm)
self.button_heartrate.pack(side=LEFT)
#FRAME 5
self.frame_five = tk.Frame(self.master)
self.frame_five.pack(fill=X)
......@@ -102,6 +136,35 @@ class Application(tk.Frame):
print("Alarma HR!")
#Method that provides reference to the user interface. It uses a queue to communicate the UI with the exterior. It returns references to
#inner objetcs so the exterior can communicate with it. More references can be given in "SimpleNameSpace"
def gui_reference(self, root, queue):
entry = self.entry_heartrate
entry["state"] = 'normal'
entry.delete(0,"end")
entry.insert(0,"10")
entry["state"] = 'disabled'
self.button_heartrate["command"] = lambda : queue.put(Messages.HRSETALARM)
#Just for testing purposes to emit reference to another widget
entry_two = self.entry_heartrate_minimum
return SimpleNamespace(entry=entry, entry_two = entry_two)
if __name__ == '__main__':
root = tk.Tk()
app = Application(master=root)
#Queue used to communicate the UI with the exterior
guiQueue = queue.Queue()
guiRef = app.gui_reference(root, guiQueue)
model = SimpleNamespace(count=0)
#Thread used to separate the use interface with the front end. It is used to not slow down the GUI when processing information
t = Thread(target=refreshUI, args=(guiRef, model, guiQueue,))
t.daemon = True
t.start()
app.mainloop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment