From 3faee283f3e56f046e106fff5330a756a949618d Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Tue, 23 Dec 2025 21:24:30 +0100 Subject: Improve layout and add ruff configuration --- src/scorecounter/app.py | 102 +++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/scorecounter/app.py b/src/scorecounter/app.py index cf4bc1d..26a0c74 100644 --- a/src/scorecounter/app.py +++ b/src/scorecounter/app.py @@ -1,7 +1,7 @@ """Simple score counter for two teams""" import toga -from toga.style.pack import COLUMN, ROW +from toga.constants import COLUMN, ROW class ScoreCounter(toga.App): @@ -9,65 +9,97 @@ class ScoreCounter(toga.App): def startup(self): """startup entry point""" - self.score_team1 = 0 - self.score_team2 = 0 + self._score_team1 = 0 + self._score_team2 = 0 - main_box = toga.Box(direction=COLUMN) + self.main_window = toga.MainWindow( + title=self.formal_name, on_resize=self.cb_main_window_resize + ) - self.score_label1 = toga.Label( - f'Team1: {self.score_team1}', margin=(0, 5), color='red' + self._score_label1 = toga.Label( + f'Team1: {self._score_team1}', + margin=(0, 5), + color='red', + font_weight='bold', + font_size=24, + ) + self._score_label2 = toga.Label( + f'Team2: {self._score_team2}', + margin=(0, 5), + color='blue', + font_weight='bold', + font_size=24, ) - self.score_label2 = toga.Label( - f'Team2: {self.score_team2}', margin=(0, 5), color='blue' + separator_label = toga.Label('', flex=1) + score_box = toga.Box( + direction=ROW, + margin=5, + children=[self._score_label1, separator_label, self._score_label2], ) - self.separator_label = toga.Label(' - ', margin=(0, 10)) - score_box = toga.Box(direction=ROW, margin=5) - score_box.add(self.score_label1) - score_box.add(self.separator_label) - score_box.add(self.score_label2) + # empty box for consuming vertical space + empty_box = toga.Box(flex=1) - button_clear = toga.Button( - 'Clear', on_press=self.cb_clear, margin=(20, 5, 5) + self._button_team1 = toga.Button( + 'Team 1 (+1)', + on_press=self.cb_score_team1, + background_color='red', + flex=1, + font_size=18, + color='white', + ) + self._button_team2 = toga.Button( + 'Team 2 (+1)', + on_press=self.cb_score_team2, + background_color='blue', + flex=1, + font_size=18, + color='white', + ) + button_box = toga.Box( + direction=COLUMN, + children=[self._button_team1, self._button_team2], + margin=(20, 5, 0), ) - button_team1 = toga.Button( - 'Team 1 (+1)', on_press=self.cb_score_team1, margin=(20, 5, 5) + + self.main_window.content = toga.Box( + direction=COLUMN, children=[score_box, empty_box, button_box] ) - button_team1.style.background_color = 'red' - button_team2 = toga.Button( - 'Team 2 (+1)', on_press=self.cb_score_team2, margin=5 + self._button_team1.style.height = int(self.main_window.size.height / 5) + self._button_team2.style.height = int(self.main_window.size.height / 5) + + command_clear = toga.Command( + self.cb_clear, text='Clear', group=toga.Group('Scoreboard') ) - button_team2.style.background_color = 'blue' - - main_box.add(score_box) - main_box.add(button_team1) - main_box.add(button_team2) - main_box.add(button_clear) - - self.main_window = toga.MainWindow(title=self.formal_name) - self.main_window.content = main_box + self.commands.add(command_clear) + self.main_window.show() def cb_clear(self, _widget): """Clear on press callback""" - self.score_team1 = 0 - self.score_team2 = 0 + self._score_team1 = 0 + self._score_team2 = 0 self.refresh() def cb_score_team1(self, _widget): """Team1 on press callback""" - self.score_team1 += 1 + self._score_team1 += 1 self.refresh() def cb_score_team2(self, _widget): """Team 2 on press callback""" - self.score_team2 += 1 + self._score_team2 += 1 self.refresh() + def cb_main_window_resize(self, widget) -> None: + """On main window resize callback""" + self._button_team1.style.height = int(widget.size.height / 5) + self._button_team2.style.height = int(widget.size.height / 5) + def refresh(self): """Refreshes the score labels""" - self.score_label1.text = f'Team1: {self.score_team1}' - self.score_label2.text = f'Team2: {self.score_team2}' + self._score_label1.text = f'Team1: {self._score_team1}' + self._score_label2.text = f'Team2: {self._score_team2}' def main(): -- cgit v1.3