-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_calculator.py
More file actions
97 lines (71 loc) · 4.81 KB
/
Copy pathGUI_calculator.py
File metadata and controls
97 lines (71 loc) · 4.81 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
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
import tkinter as tk # standard Python interface to create graphical user interfaces (GUIs)
calculation = "" #store the ongoing input from the user as they enter numbers and operators.
def add_to_calculation(symbol):
global calculation
calculation += str(symbol) # converts the symbol to a string and appends it to the ongoing calculation.
text_result.delete(1.0, "end") # used in many GUI applications to clear output or reset a text area before inserting new data.
text_result.insert(1.0, calculation) #Inserts the current calculation string into the display area starting from the first position.
def evalute_calculation():# the expression stored in the calculation variable when the user presses the "=" button
global calculation
try:
calculation = str(eval(calculation))
#Python's eval() function,If an error occurs, it clears the field and shows "Error". However, be careful with eval() due to potential security risks if users input malicious code.
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
except:
clear_field()
text_result.insert(1.0, "Error")
def clear_field():# used to clear the input field, resetting the calculator to its initial state when the user presses the "C" button.
global calculation
calculation = ""
text_result.delete(1.0, "end")
def backspace(): # Add a backspace functionality to remove the last character of the current calculation.
global calculation
calculation = calculation[:-1]
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
root = tk.Tk()# initialize the main application window.
root.geometry("323x300")#dimensions of the calculator window to 323 pixels wide and 359 pixels tall. The * should be replaced by x (the correct separator for specifying dimensions).
root.title("GUI Calculator using tkinter")
text_result = tk.Text(root, height =2, width=18, font=("Aerial",23))
text_result.grid(columnspan=5)#Creates a text widget (display area) with specified height, width, and font.
btn_1 = tk.Button(root, text="1", command=lambda:add_to_calculation(1), width=5, font=("Aerial",14))
btn_1.grid(row=2, column=1)
#Buttons like btn_1, btn_2, etc., are created using tk.Button() and configured to trigger the add_to_calculation() function with specific numbers or operators. grid() arranges them on the screen.
btn_2 = tk.Button(root, text="2", command=lambda:add_to_calculation (2), width=5, font=("Aerial",14))
btn_2.grid(row=2, column=2)
btn_3 = tk.Button(root, text="3", command=lambda:add_to_calculation(3), width=5, font=("Aerial",14))
btn_3.grid(row=2, column=3)
btn_4 = tk.Button(root, text="4", command=lambda:add_to_calculation(4), width=5, font=("Aerial",14))
btn_4.grid(row=3, column=1)
btn_5 = tk.Button(root, text="5", command=lambda:add_to_calculation(5), width=5, font=("Aerial",14))
btn_5.grid(row=3, column=2)
btn_6 = tk.Button(root, text="6", command=lambda:add_to_calculation(6), width=5, font=("Aerial",14))
btn_6.grid(row=3, column=3)
btn_7 = tk.Button(root, text="7", command=lambda:add_to_calculation(7), width=5, font=("Aerial",14))
btn_7.grid(row=4, column=1)
btn_8 = tk.Button(root, text="8", command=lambda:add_to_calculation(8), width=5, font=("Aerial",14))
btn_8.grid(row=4, column=2)
btn_9 = tk.Button(root, text="9", command=lambda:add_to_calculation(9), width=5, font=("Aerial",14))
btn_9.grid(row=4, column=3)
btn_0 = tk.Button(root, text="0", command=lambda:add_to_calculation(0), width=5, font=("Aerial",14))
btn_0.grid(row=5, column=2)
btn_plus = tk.Button(root, text="+", command=lambda:add_to_calculation("+"), width=5, font=("Aerial",14))
btn_plus.grid(row=2, column=4)
btn_minus = tk.Button(root, text="-", command=lambda:add_to_calculation("-"), width=5, font=("Aerial",14))
btn_minus.grid(row=3, column=4)
btn_mul = tk.Button(root, text="*", command=lambda:add_to_calculation("*"), width=5, font=("Aerial",14))
btn_mul.grid(row=4, column=4)
btn_div = tk.Button(root, text="/", command=lambda:add_to_calculation("/"), width=5, font=("Aerial",14))
btn_div.grid(row=5, column=4)
btn_open = tk.Button(root, text="(", command=lambda:add_to_calculation("("), width=5, font=("Aerial",14))
btn_open.grid(row=5, column=1 )
btn_close = tk.Button(root, text=")", command=lambda:add_to_calculation(")"), width=5, font=("Aerial",14))
btn_close.grid(row=5, column=3)
btn_equals = tk.Button(root, text="=", command=evalute_calculation, width=6, font=("Aerial",14))
btn_equals.grid(row=6, column=2,columnspan=2)
btn_clear = tk.Button(root, text="C", command=clear_field, width=6, font=("Aerial",14))
btn_clear.grid(row=6, column=0,columnspan=2)
btn_clearbutton = tk.Button(root, text="Back", command=backspace, width=6, font=("Aerial",14))
btn_clearbutton.grid(row=6, column=4, columnspan=2)
root.mainloop()#keeps the window open and allows the GUI to respond to user inputs.