Amazing-Python-Scripts
190 строк · 5.5 Кб
1import tkinter as tk
2
3root = tk.Tk() # Main box window
4root.title("Standard Calculator") # Title shown at the title bar
5root.resizable(0, 0) # disabling the resizeing of the window
6
7# Creating an entry field:
8e = tk.Entry(root,
9width=35,
10bg='#f0ffff',
11fg='black',
12borderwidth=5,
13justify='right',
14font='Calibri 15')
15e.grid(row=0, column=0, columnspan=3, padx=12, pady=12)
16
17
18def buttonClick(num): # function for clicking
19temp = e.get(
20) # temporary varibale to store the current input in the screen
21e.delete(0, tk.END) # clearing the screen from index 0 to END
22e.insert(0, temp + num) # inserting the incoming number input
23
24
25def buttonClear(): # function for clearing
26e.delete(0, tk.END)
27
28
29def buttonGet(
30oper
31): # function for storing the first input and printing '+, -, /, *'
32global num1, math # global variable num1 and math to use in function buttonEqual()
33num1 = e.get() # getting first number
34math = oper # oper varaible is the type of operation being performed
35e.insert(tk.END, math)
36try:
37num1 = float(num1) # converting the number to float type
38except ValueError: # in case there is a character other than numerals, clear the screen
39buttonClear()
40
41
42def buttonEqual(): # function for printing the sum
43inp = e.get() # getting the inserted input
44num2 = float(inp[inp.index(math) + 1:]) # getting the second number
45e.delete(0, tk.END)
46if math == '+': # Addition
47e.insert(0, str(num1 + num2))
48elif math == '-': # Subtraction
49e.insert(0, str(num1 - num2))
50elif math == 'x': # Multiplication
51e.insert(0, str(num1 * num2))
52elif math == '/': # Division
53try:
54e.insert(0, str(num1 / num2))
55except ZeroDivisionError:
56# in case there is a zero in the denominator, answer is undefined
57e.insert(0, 'Undefined')
58
59
60# Defining Buttons:
61b1 = tk.Button(root,
62text='1',
63padx=40,
64pady=10,
65command=lambda: buttonClick('1'),
66font='Calibri 12')
67b2 = tk.Button(root,
68text='2',
69padx=40,
70pady=10,
71command=lambda: buttonClick('2'),
72font='Calibri 12')
73b3 = tk.Button(root,
74text='3',
75padx=40,
76pady=10,
77command=lambda: buttonClick('3'),
78font='Calibri 12')
79b4 = tk.Button(root,
80text='4',
81padx=40,
82pady=10,
83command=lambda: buttonClick('4'),
84font='Calibri 12')
85b5 = tk.Button(root,
86text='5',
87padx=40,
88pady=10,
89command=lambda: buttonClick('5'),
90font='Calibri 12')
91b6 = tk.Button(root,
92text='6',
93padx=40,
94pady=10,
95command=lambda: buttonClick('6'),
96font='Calibri 12')
97b7 = tk.Button(root,
98text='7',
99padx=40,
100pady=10,
101command=lambda: buttonClick('7'),
102font='Calibri 12')
103b8 = tk.Button(root,
104text='8',
105padx=40,
106pady=10,
107command=lambda: buttonClick('8'),
108font='Calibri 12')
109b9 = tk.Button(root,
110text='9',
111padx=40,
112pady=10,
113command=lambda: buttonClick('9'),
114font='Calibri 12')
115b0 = tk.Button(root,
116text='0',
117padx=40,
118pady=10,
119command=lambda: buttonClick('0'),
120font='Calibri 12')
121bdot = tk.Button(root,
122text='.',
123padx=41,
124pady=10,
125command=lambda: buttonClick('.'),
126font='Calibri 12')
127
128badd = tk.Button(root,
129text='+',
130padx=29,
131pady=10,
132command=lambda: buttonGet('+'),
133font='Calibri 12')
134bsub = tk.Button(root,
135text='-',
136padx=30,
137pady=10,
138command=lambda: buttonGet('-'),
139font='Calibri 12')
140bmul = tk.Button(root,
141text='x',
142padx=30,
143pady=10,
144command=lambda: buttonGet('x'),
145font='Calibri 12')
146bdiv = tk.Button(root,
147text='/',
148padx=30.5,
149pady=10,
150command=lambda: buttonGet('/'),
151font='Calibri 12')
152
153bclear = tk.Button(root,
154text='AC',
155padx=20,
156pady=10,
157command=buttonClear,
158font='Calibri 12')
159bequal = tk.Button(root,
160text='=',
161padx=39,
162pady=10,
163command=buttonEqual,
164font='Calibri 12')
165
166# Putting the buttons on the screen:
167b1.grid(row=3, column=0)
168b2.grid(row=3, column=1)
169b3.grid(row=3, column=2)
170badd.grid(row=3, column=3)
171
172b4.grid(row=2, column=0)
173b5.grid(row=2, column=1)
174b6.grid(row=2, column=2)
175bmul.grid(row=2, column=3)
176
177b7.grid(row=1, column=0)
178b8.grid(row=1, column=1)
179b9.grid(row=1, column=2)
180bdiv.grid(row=1, column=3)
181
182b0.grid(row=4, column=0)
183bdot.grid(row=4, column=1)
184bequal.grid(row=4, column=2)
185bsub.grid(row=4, column=3)
186
187bclear.grid(row=0, column=3)
188
189# Looping the window:
190root.mainloop()
191