pygui updated progress tasks to be self contained and leverage a title value to display runtime with more context to user

This commit is contained in:
Blake Harnden 2020-05-03 21:47:58 -07:00
parent 1dd45f4424
commit 4ec6ef25fe
5 changed files with 40 additions and 30 deletions

View file

@ -1,20 +1,34 @@
import logging
import threading
from typing import Any, Callable, Tuple
import time
from typing import TYPE_CHECKING, Any, Callable, Tuple
if TYPE_CHECKING:
from core.gui.app import Application
class ProgressTask:
def __init__(
self, task: Callable, callback: Callable = None, args: Tuple[Any] = None
self,
app: "Application",
title: str,
task: Callable,
callback: Callable = None,
args: Tuple[Any] = None,
):
self.app = None
self.app = app
self.title = title
self.task = task
self.callback = callback
self.args = args
if self.args is None:
self.args = ()
self.time = None
def start(self) -> None:
self.app.progress.grid(sticky="ew")
self.app.progress.start()
self.time = time.perf_counter()
thread = threading.Thread(target=self.run, daemon=True)
thread.start()
@ -33,4 +47,12 @@ class ProgressTask:
logging.exception("progress task exception")
self.app.show_exception("Task Error", e)
finally:
self.app.after(0, self.app.progress_task_complete)
self.app.after(0, self.complete)
def complete(self):
self.app.progress.stop()
self.app.progress.grid_forget()
total = time.perf_counter() - self.time
self.time = None
message = f"{self.title} ran for {total:.3f} seconds"
self.app.statusbar.set_status(message)