-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_briefcase.py
More file actions
69 lines (56 loc) · 1.89 KB
/
main_briefcase.py
File metadata and controls
69 lines (56 loc) · 1.89 KB
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
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
class Calculator(toga.App):
def startup(self):
self.current_input = ""
self.last_operator = None
self.last_value = None
main_box = toga.Box(style=Pack(direction=COLUMN, padding=10))
# 显示屏
self.display = toga.TextInput(
readonly=True,
style=Pack(padding=5, flex=1, font_size=24)
)
main_box.add(self.display)
# 按钮布局
buttons = [
['7', '8', '9', '/'],
['4', '5', '6', '*'],
['1', '2', '3', '-'],
['0', '.', 'C', '+'],
['=']
]
for row in buttons:
row_box = toga.Box(style=Pack(direction=ROW, padding=2))
for label in row:
btn = toga.Button(
label,
on_press=self.on_button_press,
style=Pack(flex=1, padding=2, font_size=18)
)
row_box.add(btn)
main_box.add(row_box)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def on_button_press(self, widget):
label = widget.label
if label == 'C':
self.current_input = ""
self.display.value = ""
elif label == '=':
try:
result = str(eval(self.current_input))
self.display.value = result
self.current_input = result
except:
self.display.value = "Error"
self.current_input = ""
else:
self.current_input += label
self.display.value = self.current_input
def main():
return Calculator()
if __name__ == '__main__':
main().main_loop()