-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcalculator.py
More file actions
63 lines (52 loc) · 1.89 KB
/
calculator.py
File metadata and controls
63 lines (52 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
import PySimpleGUI as sg
def create_window(theme):
sg.theme(theme)
sg.set_options(font = 'Franklin 14', button_element_size = (6,3))
button_size = (6,3)
layout = [
[sg.Text(
'',
font = 'Franklin 26',
justification = 'right',
expand_x = True,
pad = (10,20),
right_click_menu = theme_menu,
key = '-TEXT-')
],
[sg.Button('Clear', expand_x = True), sg.Button('Enter', expand_x = True)],
[sg.Button(7, size = button_size),sg.Button(8, size = button_size),sg.Button(9, size = button_size),sg.Button('*', size = button_size)],
[sg.Button(4, size = button_size),sg.Button(5, size = button_size),sg.Button(6, size = button_size),sg.Button('/', size = button_size)],
[sg.Button(1, size = button_size),sg.Button(2, size = button_size),sg.Button(3, size = button_size),sg.Button('-', size = button_size)],
[sg.Button(0, expand_x = True),sg.Button('.', size = button_size),sg.Button('+', size = button_size)],
]
return sg.Window('Calculator', layout)
theme_menu = ['menu',['LightGrey1','dark','DarkGray8','random']]
window = create_window('dark')
current_num = []
full_operation = []
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event in theme_menu[1]:
window.close()
window = create_window(event)
if event in ['0','1','2','3','4','5','6','7','8','9','.']:
current_num.append(event)
num_string = ''.join(current_num)
window['-TEXT-'].update(num_string)
if event in ['+','-','/','*']:
full_operation.append(''.join(current_num))
current_num = []
full_operation.append(event)
window['-TEXT-'].update('')
if event == 'Enter':
full_operation.append(''.join(current_num))
result = eval(' '.join(full_operation))
window['-TEXT-'].update(result)
full_operation = []
if event == 'Clear':
current_num = []
full_operation = []
window['-TEXT-'].update('')
window.close()