ProyectoFinalPython/app/widgets/abc/ThreadedLabel.py

29 lines
831 B
Python

import threading
import time
from abc import ABC, abstractmethod
from threading import Thread
from tkinter import Label
class ThreadedLabel(ABC, Label):
def __init__(self, root: Label, stop_event: threading.Event, refresh_rate = None, **kwargs):
super().__init__(root, **kwargs)
self.stop_event = stop_event
self.refresh_rate = refresh_rate
self.declared_thread: Thread = threading.Thread(target=self.__loop)
self.declared_thread.start()
def __loop(self):
while not self.stop_event.is_set():
self.task()
start = time.time()
while time.time() - start < self.refresh_rate:
if self.stop_event.is_set():
break
time.sleep(0.2)
@abstractmethod
def task(self, *args):
pass