Small GUI internals cleanup

This commit is contained in:
DevilXD
2022-03-09 11:06:43 +01:00
parent 5b04907d33
commit 2bddd68af2
2 changed files with 15 additions and 14 deletions

21
gui.py
View File

@@ -1,7 +1,5 @@
from __future__ import annotations
import os
import sys
import asyncio
import logging
import tkinter as tk
@@ -16,6 +14,7 @@ try:
except ModuleNotFoundError as exc:
raise ImportError("You have to run 'pip install pystray' first") from exc
from utils import resource_path
from constants import FORMATTER, WS_TOPICS_LIMIT, MAX_WEBSOCKETS, WINDOW_TITLE, State
if TYPE_CHECKING:
@@ -29,13 +28,7 @@ digits = ceil(log10(WS_TOPICS_LIMIT))
WS_FONT = ("Courier New", 10)
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
class ICOImage:
class _ICOImage:
def __init__(self, path: str):
with open(path, 'rb') as file:
self._data = file.read()
@@ -44,7 +37,7 @@ class ICOImage:
file.write(self._data)
class TKOutputHandler(logging.Handler):
class _TKOutputHandler(logging.Handler):
def __init__(self, output: GUIManager):
super().__init__()
self._output = output
@@ -516,7 +509,7 @@ class ConsoleOutput:
self._text.config(state="disabled")
class Buttons(TypedDict):
class _Buttons(TypedDict):
frame: ttk.Frame
switch: ttk.Button
load_points: ttk.Button
@@ -532,7 +525,7 @@ class ChannelList:
# tell master frame that the containing column can expand
master.columnconfigure(2, weight=1)
buttons_frame = ttk.Frame(frame)
self._buttons: Buttons = {
self._buttons: _Buttons = {
"frame": buttons_frame,
"switch": ttk.Button(
buttons_frame,
@@ -788,7 +781,7 @@ class TrayIcon:
)
self.icon = pystray.Icon(
"twitch_miner",
ICOImage(resource_path("pickaxe.ico")),
_ICOImage(resource_path("pickaxe.ico")),
self.get_title(drop),
menu,
)
@@ -910,7 +903,7 @@ class GUIManager:
root.update_idletasks()
root.minsize(width=0, height=root.winfo_reqheight())
# register logging handler
self._handler = TKOutputHandler(self)
self._handler = _TKOutputHandler(self)
self._handler.setFormatter(FORMATTER)
logging.getLogger("TwitchDrops").addHandler(self._handler)
# show the window when ready

View File

@@ -1,5 +1,7 @@
from __future__ import annotations
import os
import sys
import random
import string
import asyncio
@@ -28,6 +30,12 @@ logger = logging.getLogger("TwitchDrops")
NONCE_CHARS = string.ascii_letters + string.digits
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
def timestamp(string: str) -> datetime:
return datetime.strptime(string, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)