-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIcalculator.py
115 lines (86 loc) · 3.58 KB
/
UIcalculator.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import tkinter as tk
from tkinter import messagebox
def add_digit(digit):
value = calc.get()
if value[0] == '0' and len(value) == 1:
value = value[1:]
calc.delete(0, tk.END)
calc.insert(0, value + digit)
def add_operation(operation):
value = calc.get()
if value[-1] in '-+/*':
value = value[:-1]
elif '+' in value or '-' in value or '*' in value or '/' in value:
calculate()
value = calc.get()
calc.delete(0, tk.END)
calc.insert(0, value + operation)
def calculate():
value = calc.get()
if value[-1] in '+-*/':
value = value + value[:-1]
calc.delete(0, tk.END)
try:
calc.insert(0, eval(value))
except (NameError, SyntaxError):
messagebox.showinfo('Warning', 'Only numbers!')
calc.insert(0, 0)
except ZeroDivisionError:
messagebox.showinfo('Warning', 'div to zero is wrong')
calc.insert(0, 0)
def clear():
calc.delete(0, tk.END)
calc.insert(0, 0)
def make_digit_button(digit):
return tk.Button(text=digit, bd=5, font=('Arial', 13), command=lambda: add_digit(digit))
def make_operation_button(operation):
return tk.Button(text=operation, bd=5, font=('Arial', 13), fg='red',
command=lambda: add_operation(operation))
def make_calc_button(operation):
return tk.Button(text=operation, bd=5, font=('Arial', 13), fg='red',
command=calculate)
def make_clear_button(operation):
return tk.Button(text=operation, bd=5, font=('Arial', 13), fg='red',
command=clear)
def press_key(event):
print(repr(event.char))
if event.char.isdigit():
add_digit(event.char)
elif event.char in '+-*/':
add_operation(event.char)
elif event.char == '\r':
calculate()
win = tk.Tk()
win.geometry(f'240x270+100+200')
win.resizable(False, False)
win['bg'] = '#fdaf66'
win.title('Calculator')
win.bind('<Key>', press_key)
calc = tk.Entry(win, justify=tk.RIGHT, font=('Arial', 15), width=15)
calc.insert(0, '0')
calc.grid(row=0, column=0, columnspan=4, stick='we', padx=5)
make_digit_button('1').grid(row=1, column=0, stick='wens', padx=5, pady=5)
make_digit_button('2').grid(row=1, column=1, stick='wens', padx=5, pady=5)
make_digit_button('3').grid(row=1, column=2, stick='wens', padx=5, pady=5)
make_digit_button('4').grid(row=2, column=0, stick='wens', padx=5, pady=5)
make_digit_button('5').grid(row=2, column=1, stick='wens', padx=5, pady=5)
make_digit_button('6').grid(row=2, column=2, stick='wens', padx=5, pady=5)
make_digit_button('7').grid(row=3, column=0, stick='wens', padx=5, pady=5)
make_digit_button('8').grid(row=3, column=1, stick='wens', padx=5, pady=5)
make_digit_button('9').grid(row=3, column=2, stick='wens', padx=5, pady=5)
make_digit_button('0').grid(row=4, column=0, stick='wens', padx=5, pady=5)
make_operation_button('+').grid(row=1, column=3, stick='wens', padx=5, pady=5)
make_operation_button('-').grid(row=2, column=3, stick='wens', padx=5, pady=5)
make_operation_button('/').grid(row=3, column=3, stick='wens', padx=5, pady=5)
make_operation_button('*').grid(row=4, column=3, stick='wens', padx=5, pady=5)
make_calc_button('=').grid(row=4, column=2, stick='wens', padx=5, pady=5)
make_clear_button('C').grid(row=4, column=1, stick='wens', padx=5, pady=5)
win.grid_columnconfigure(0, minsize=60)
win.grid_columnconfigure(1, minsize=60)
win.grid_columnconfigure(2, minsize=60)
win.grid_columnconfigure(3, minsize=60)
win.grid_rowconfigure(1, minsize=60)
win.grid_rowconfigure(2, minsize=60)
win.grid_rowconfigure(3, minsize=60)
win.grid_rowconfigure(4, minsize=60)
win.mainloop()