diff options
| author | Simeon Simeonov | 2025-12-23 21:24:30 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2025-12-23 21:24:30 +0100 |
| commit | 3faee283f3e56f046e106fff5330a756a949618d (patch) | |
| tree | 3e5fbe91f70030f577b5012dd85c5a1a2ba4ccb5 /src | |
| parent | a29c4509a316c0c77450b6fa74b96f036b0f0821 (diff) | |
Improve layout and add ruff configuration
Diffstat (limited to 'src')
| -rw-r--r-- | src/scorecounter/app.py | 100 |
1 files changed, 66 insertions, 34 deletions
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 @@ | |||
| 1 | """Simple score counter for two teams""" | 1 | """Simple score counter for two teams""" |
| 2 | 2 | ||
| 3 | import toga | 3 | import toga |
| 4 | from toga.style.pack import COLUMN, ROW | 4 | from toga.constants import COLUMN, ROW |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | class ScoreCounter(toga.App): | 7 | class ScoreCounter(toga.App): |
| @@ -9,65 +9,97 @@ class ScoreCounter(toga.App): | |||
| 9 | 9 | ||
| 10 | def startup(self): | 10 | def startup(self): |
| 11 | """startup entry point""" | 11 | """startup entry point""" |
| 12 | self.score_team1 = 0 | 12 | self._score_team1 = 0 |
| 13 | self.score_team2 = 0 | 13 | self._score_team2 = 0 |
| 14 | 14 | ||
| 15 | main_box = toga.Box(direction=COLUMN) | 15 | self.main_window = toga.MainWindow( |
| 16 | title=self.formal_name, on_resize=self.cb_main_window_resize | ||
| 17 | ) | ||
| 16 | 18 | ||
| 17 | self.score_label1 = toga.Label( | 19 | self._score_label1 = toga.Label( |
| 18 | f'Team1: {self.score_team1}', margin=(0, 5), color='red' | 20 | f'Team1: {self._score_team1}', |
| 21 | margin=(0, 5), | ||
| 22 | color='red', | ||
| 23 | font_weight='bold', | ||
| 24 | font_size=24, | ||
| 25 | ) | ||
| 26 | self._score_label2 = toga.Label( | ||
| 27 | f'Team2: {self._score_team2}', | ||
| 28 | margin=(0, 5), | ||
| 29 | color='blue', | ||
| 30 | font_weight='bold', | ||
| 31 | font_size=24, | ||
| 19 | ) | 32 | ) |
| 20 | self.score_label2 = toga.Label( | 33 | separator_label = toga.Label('', flex=1) |
| 21 | f'Team2: {self.score_team2}', margin=(0, 5), color='blue' | 34 | score_box = toga.Box( |
| 35 | direction=ROW, | ||
| 36 | margin=5, | ||
| 37 | children=[self._score_label1, separator_label, self._score_label2], | ||
| 22 | ) | 38 | ) |
| 23 | self.separator_label = toga.Label(' - ', margin=(0, 10)) | ||
| 24 | 39 | ||
| 25 | score_box = toga.Box(direction=ROW, margin=5) | 40 | # empty box for consuming vertical space |
| 26 | score_box.add(self.score_label1) | 41 | empty_box = toga.Box(flex=1) |
| 27 | score_box.add(self.separator_label) | ||
| 28 | score_box.add(self.score_label2) | ||
| 29 | 42 | ||
| 30 | button_clear = toga.Button( | 43 | self._button_team1 = toga.Button( |
| 31 | 'Clear', on_press=self.cb_clear, margin=(20, 5, 5) | 44 | 'Team 1 (+1)', |
| 45 | on_press=self.cb_score_team1, | ||
| 46 | background_color='red', | ||
| 47 | flex=1, | ||
| 48 | font_size=18, | ||
| 49 | color='white', | ||
| 32 | ) | 50 | ) |
| 33 | button_team1 = toga.Button( | 51 | self._button_team2 = toga.Button( |
| 34 | 'Team 1 (+1)', on_press=self.cb_score_team1, margin=(20, 5, 5) | 52 | 'Team 2 (+1)', |
| 53 | on_press=self.cb_score_team2, | ||
| 54 | background_color='blue', | ||
| 55 | flex=1, | ||
| 56 | font_size=18, | ||
| 57 | color='white', | ||
| 35 | ) | 58 | ) |
| 36 | button_team1.style.background_color = 'red' | 59 | button_box = toga.Box( |
| 37 | button_team2 = toga.Button( | 60 | direction=COLUMN, |
| 38 | 'Team 2 (+1)', on_press=self.cb_score_team2, margin=5 | 61 | children=[self._button_team1, self._button_team2], |
| 62 | margin=(20, 5, 0), | ||
| 63 | ) | ||
| 64 | |||
| 65 | self.main_window.content = toga.Box( | ||
| 66 | direction=COLUMN, children=[score_box, empty_box, button_box] | ||
| 39 | ) | 67 | ) |
| 40 | button_team2.style.background_color = 'blue' | 68 | self._button_team1.style.height = int(self.main_window.size.height / 5) |
| 69 | self._button_team2.style.height = int(self.main_window.size.height / 5) | ||
| 70 | |||
| 71 | command_clear = toga.Command( | ||
| 72 | self.cb_clear, text='Clear', group=toga.Group('Scoreboard') | ||
| 73 | ) | ||
| 74 | self.commands.add(command_clear) | ||
| 41 | 75 | ||
| 42 | main_box.add(score_box) | ||
| 43 | main_box.add(button_team1) | ||
| 44 | main_box.add(button_team2) | ||
| 45 | main_box.add(button_clear) | ||
| 46 | |||
| 47 | self.main_window = toga.MainWindow(title=self.formal_name) | ||
| 48 | self.main_window.content = main_box | ||
| 49 | self.main_window.show() | 76 | self.main_window.show() |
| 50 | 77 | ||
| 51 | def cb_clear(self, _widget): | 78 | def cb_clear(self, _widget): |
| 52 | """Clear on press callback""" | 79 | """Clear on press callback""" |
| 53 | self.score_team1 = 0 | 80 | self._score_team1 = 0 |
| 54 | self.score_team2 = 0 | 81 | self._score_team2 = 0 |
| 55 | self.refresh() | 82 | self.refresh() |
| 56 | 83 | ||
| 57 | def cb_score_team1(self, _widget): | 84 | def cb_score_team1(self, _widget): |
| 58 | """Team1 on press callback""" | 85 | """Team1 on press callback""" |
| 59 | self.score_team1 += 1 | 86 | self._score_team1 += 1 |
| 60 | self.refresh() | 87 | self.refresh() |
| 61 | 88 | ||
| 62 | def cb_score_team2(self, _widget): | 89 | def cb_score_team2(self, _widget): |
| 63 | """Team 2 on press callback""" | 90 | """Team 2 on press callback""" |
| 64 | self.score_team2 += 1 | 91 | self._score_team2 += 1 |
| 65 | self.refresh() | 92 | self.refresh() |
| 66 | 93 | ||
| 94 | def cb_main_window_resize(self, widget) -> None: | ||
| 95 | """On main window resize callback""" | ||
| 96 | self._button_team1.style.height = int(widget.size.height / 5) | ||
| 97 | self._button_team2.style.height = int(widget.size.height / 5) | ||
| 98 | |||
| 67 | def refresh(self): | 99 | def refresh(self): |
| 68 | """Refreshes the score labels""" | 100 | """Refreshes the score labels""" |
| 69 | self.score_label1.text = f'Team1: {self.score_team1}' | 101 | self._score_label1.text = f'Team1: {self._score_team1}' |
| 70 | self.score_label2.text = f'Team2: {self.score_team2}' | 102 | self._score_label2.text = f'Team2: {self._score_team2}' |
| 71 | 103 | ||
| 72 | 104 | ||
| 73 | def main(): | 105 | def main(): |
