summaryrefslogtreecommitdiff
path: root/app.py
blob: cf4bc1deb9afa5321d5989f896cf124cfe0af4c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Simple score counter for two teams"""

import toga
from toga.style.pack import COLUMN, ROW


class ScoreCounter(toga.App):
    """The main ScoreCounter application"""

    def startup(self):
        """startup entry point"""
        self.score_team1 = 0
        self.score_team2 = 0

        main_box = toga.Box(direction=COLUMN)

        self.score_label1 = toga.Label(
            f'Team1: {self.score_team1}', margin=(0, 5), color='red'
        )
        self.score_label2 = toga.Label(
            f'Team2: {self.score_team2}', margin=(0, 5), color='blue'
        )
        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)

        button_clear = toga.Button(
            'Clear', on_press=self.cb_clear, margin=(20, 5, 5)
        )
        button_team1 = toga.Button(
            'Team 1 (+1)', on_press=self.cb_score_team1, margin=(20, 5, 5)
        )
        button_team1.style.background_color = 'red'
        button_team2 = toga.Button(
            'Team 2 (+1)', on_press=self.cb_score_team2, margin=5
        )
        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.main_window.show()

    def cb_clear(self, _widget):
        """Clear on press callback"""
        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.refresh()

    def cb_score_team2(self, _widget):
        """Team 2 on press callback"""
        self.score_team2 += 1
        self.refresh()

    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}'


def main():
    """main entry point"""
    return ScoreCounter()