diff options
Diffstat (limited to 'app.py')
| -rw-r--r-- | app.py | 75 |
1 files changed, 75 insertions, 0 deletions
| @@ -0,0 +1,75 @@ | |||
| 1 | """Simple score counter for two teams""" | ||
| 2 | |||
| 3 | import toga | ||
| 4 | from toga.style.pack import COLUMN, ROW | ||
| 5 | |||
| 6 | |||
| 7 | class ScoreCounter(toga.App): | ||
| 8 | """The main ScoreCounter application""" | ||
| 9 | |||
| 10 | def startup(self): | ||
| 11 | """startup entry point""" | ||
| 12 | self.score_team1 = 0 | ||
| 13 | self.score_team2 = 0 | ||
| 14 | |||
| 15 | main_box = toga.Box(direction=COLUMN) | ||
| 16 | |||
| 17 | self.score_label1 = toga.Label( | ||
| 18 | f'Team1: {self.score_team1}', margin=(0, 5), color='red' | ||
| 19 | ) | ||
| 20 | self.score_label2 = toga.Label( | ||
| 21 | f'Team2: {self.score_team2}', margin=(0, 5), color='blue' | ||
| 22 | ) | ||
| 23 | self.separator_label = toga.Label(' - ', margin=(0, 10)) | ||
| 24 | |||
| 25 | score_box = toga.Box(direction=ROW, margin=5) | ||
| 26 | score_box.add(self.score_label1) | ||
| 27 | score_box.add(self.separator_label) | ||
| 28 | score_box.add(self.score_label2) | ||
| 29 | |||
| 30 | button_clear = toga.Button( | ||
| 31 | 'Clear', on_press=self.cb_clear, margin=(20, 5, 5) | ||
| 32 | ) | ||
| 33 | button_team1 = toga.Button( | ||
| 34 | 'Team 1 (+1)', on_press=self.cb_score_team1, margin=(20, 5, 5) | ||
| 35 | ) | ||
| 36 | button_team1.style.background_color = 'red' | ||
| 37 | button_team2 = toga.Button( | ||
| 38 | 'Team 2 (+1)', on_press=self.cb_score_team2, margin=5 | ||
| 39 | ) | ||
| 40 | button_team2.style.background_color = 'blue' | ||
| 41 | |||
| 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() | ||
| 50 | |||
| 51 | def cb_clear(self, _widget): | ||
| 52 | """Clear on press callback""" | ||
| 53 | self.score_team1 = 0 | ||
| 54 | self.score_team2 = 0 | ||
| 55 | self.refresh() | ||
| 56 | |||
| 57 | def cb_score_team1(self, _widget): | ||
| 58 | """Team1 on press callback""" | ||
| 59 | self.score_team1 += 1 | ||
| 60 | self.refresh() | ||
| 61 | |||
| 62 | def cb_score_team2(self, _widget): | ||
| 63 | """Team 2 on press callback""" | ||
| 64 | self.score_team2 += 1 | ||
| 65 | self.refresh() | ||
| 66 | |||
| 67 | def refresh(self): | ||
| 68 | """Refreshes the score labels""" | ||
| 69 | self.score_label1.text = f'Team1: {self.score_team1}' | ||
| 70 | self.score_label2.text = f'Team2: {self.score_team2}' | ||
| 71 | |||
| 72 | |||
| 73 | def main(): | ||
| 74 | """main entry point""" | ||
| 75 | return ScoreCounter() | ||
