Created
April 24, 2024 03:39
-
-
Save nenodias/b10d85ac03f77be7c5aec2e62294afb6 to your computer and use it in GitHub Desktop.
Revisions
-
nenodias created this gist
Apr 24, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ from dataclasses import dataclass from threading import Thread, Lock from tkinter import * from tkinter import messagebox from time import sleep from typing import Any STATE = "state" ENABLED = "normal" DISABLED = "disabled" @dataclass class Command: args: dict callable: callable class AppState: def __init__(self) -> None: self.count = 0 self.running = True self.lock = Lock() self.commands = [] def __call__(self, *args: Any, **kwds: Any) -> Any: callback = [] try: while self.running: if len(self.commands) > 0: for value in self.commands: cmd :Command = value cmd.callable() if cmd.args is not None: callback.append(lambda : cmd.args.config(state = ENABLED)) else: for after in callback: after() self.commands.clear() callback.clear() except Exception as ex: print(ex) pass def clicou(self): inicio = self.count while True: self.count += 1 conta_label.config(text=f"contando: {self.count}") print(self.count) sleep(1) if self.count >= (inicio + 10): break def add_clicou(self, botao): self.lock.acquire() botao[STATE] = DISABLED self.commands.append(Command(botao, lambda: self.clicou())) self.lock.release() def close(self): self.running = False state = AppState() Thread(target=state).start() def on_closing(): if messagebox.askokcancel("Quit", "Do you want to quit?"): state.close() janela.destroy() janela = Tk() janela.geometry("200x100") conta_label = Label(janela, text="contando: 0") conta_label.grid(row=8, column=0, padx=8, pady=15) botao = Button(janela, text="Iniciar", command=lambda : state.add_clicou(botao)) botao.grid(column=0, row=10, padx=15, pady=15) janela.protocol("WM_DELETE_WINDOW", on_closing) janela.mainloop()