-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4. Calculator
More file actions
66 lines (59 loc) · 1.6 KB
/
Copy path4. Calculator
File metadata and controls
66 lines (59 loc) · 1.6 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
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
root = Tk()
root.title("Calculator")
#The calculator logic
def calc(key):
global memory
if key == "=":
#Exclude writing letters
str1 = "-+123456789.*"
if calc_entry.get()[0] not in str1:
calc_entry.insert(END, "The first character is not a number")
messagebox.showerror("Error", "You entered a wrong number")
#Account
try:
result = eval(calc_entry.get())
calc_entry.insert(END, "=" + str(result))
except:
calc_entry.insert(END, "Error")
messagebox.showerror("Error", "Check that the data is correct")
#Clear the field
elif key == "C":
calc_entry.delete(0, END)
#Change +-
elif key == "-/+":
if "=" in calc_entry.get():
calc_entry.delete(0, END)
try:
if calc_entry.get() [0] == "-":
calc_entry.delete(0)
else:
calc_entry.insert(0, "-")
except IndexError:
pass
else:
if "=" in calc_entry.get():
calc_entry.delete(0, END)
calc_entry.insert(END, key)
#Create all the buttons
bttn_list = [
"7", "8", "9", "+", "-",
"4", "5", "6", "*", "/",
"1", "2", "3", "-/+", "=",
"0", ".", "C"
]
r = 1
c = 0
for i in bttn_list:
rel = ""
cmd=lambda x=i: calc(x)
ttk.Button(root, text=i, command=cmd).grid(row=r, column=c)
c += 1
if c>4:
c=0
r += 1
calc_entry = Entry(root, width=33)
calc_entry.grid(row=0, column=0, columnspan=5)
root.mainloop()