May-29-2021, 07:36 PM
Try this, I can't test it so fingers crossed, I don't know if
data.split(",")
will be in the right order for self.form_frame.set_str_variables
import functools import tkinter as tk from concurrent import futures from digi.xbee.devices import XBeeDevice PORT = "COM5" BAUD_RATE = 9600 device = XBeeDevice(PORT, BAUD_RATE) thread_pool_executor = futures.ThreadPoolExecutor(max_workers=1) def tk_after(target): @functools.wraps(target) def wrapper(self, *args, **kwargs): args = (self,) + args self.after(0, target, *args, **kwargs) return wrapper def submit_to_pool_executor(executor): '''Decorates a method to be sumbited to the passed in executor''' def decorator(target): @functools.wraps(target) def wrapper(*args, **kwargs): result = executor.submit(target, *args, **kwargs) result.add_done_callback(executor_done_call_back) return result return wrapper return decorator def executor_done_call_back(future): exception = future.exception() if exception: raise exception class App(tk.Tk): def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.title("Sistema de Monitoreo de Paneles Solares del SESLab") self.config(bg="#919F89") width_of_window = 600 height_of_window = 280 screen_width = self.winfo_screenwidth() screen_height = self.winfo_screenheight() x_coordinate = (screen_width/2)-(width_of_window/2) y_coordinate = (screen_height/2)-(height_of_window/2) self.geometry((f'{width_of_window}x{height_of_window}+' f'{x_coordinate:.0f}+{y_coordinate:.0f}')) self.form_frame = FormFrame(self) self.form_frame.pack() self.btn = tk.Button( self, bg="#E2BBAC", text="Iniciar ", command=self.on_btn) self.btn.pack() def btn_enable(self, enable=True): self.btn.config(state='normal' if enable else 'disabled') def on_btn(self): self.btn_enable(False) self.update() @submit_to_pool_executor(thread_pool_executor) def update(self): device.open() DATA_TO_SEND = "Hola XBee!" device.send_data_broadcast(DATA_TO_SEND) try: device.flush_queues() while True: xbee_message = device.read_data() if xbee_message is not None: data = xbee_message.data.decode() self.form_frame.set_str_variables(data.split(",")) finally: if device is not None and device.is_open(): device.close() self.btn_enable() class FormFrame(tk.Frame): def __init__(self, *args, **kwargs) -> None: kwargs['width'] = '1200' kwargs['height'] = '600' super().__init__(*args, **kwargs) self.config(bg="#98A7AC") self.str_variables = [] for row_index in (1, 2, 4, 5, 6, 7, 9): str_variable = tk.StringVar() entry = tk.Entry(self, textvariable=str_variable) entry.grid(row=row_index, column=1, padx=1, pady=5) entry.config(fg="black", justify="center", state='readonly') self.str_variables.append(str_variable) label1 = tk.Label( self, text="Sistema de monitoreo de Paneles del SESLab", font=18) label1.grid(row=0, column=0, padx=5, pady=5, columnspan=2) for row_index, text in ( (1, 'Fecha de medición : '), (2, 'Hora de medición : '), (4, 'Voltage de Panel 1 : '), (5, 'Voltage de Panel 2 : '), (6, 'Voltage de Panel 3 : '), (7, 'Promedio : '), (9, 'Panel con Menor Voltaje : ')): label = tk.Label(self, text="Fecha de medición : ") label.grid(row=row_index, column=0, padx=2, pady=5) @tk_after def set_str_variables(self, results): for str_variable, result in zip(self.str_variables, results): str_variable.set(result) if __name__ == '__main__': app = App() app.mainloop()