Python code for GUI Calculator
This python code creates a basic GUI calculator. It will create a basic calculator GUI with four radiobuttons to choose the operator, two Entry widgets to input the numbers and a button to perform the calculation. The result will be displayed as a label.
import tkinter as tk
We import the tkinter library, which is the standard GUI library for Python.
def perform_calculation():
# get the value of first and second number
num1 = int(first_number_entry.get())
num2 = int(second_number_entry.get())
The perform_calculation function retrieves the values of the first and second numbers from the Entry widgets and converts them to integers.
# get the operator
operator = operator_var.get()
The operator is retrieved from the operator_var variable, which is a StringVar object.
# perform the calculation
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
result = "Invalid operator"
The calculation is performed based on the operator. If the operator is not one of the four supported operations (+, -, *, /), the function returns “Invalid operator”.
# display the result
result_label.config(text="Result: " + str(result))
The result is displayed in the result label. The config method is used to update the text of the label.
# create the main window
root = tk.Tk()
root.title("Calculator")
root.geometry("300x200")
We create the main window of the calculator application and set its title and size.
# create the first number label and entry
first_number_label = tk.Label(root, text="First Number")
first_number_label.pack()
first_number_entry = tk.Entry(root)
first_number_entry.pack()
We create a label and an Entry widget for the first number and arrange them on the window using the pack method.
# create the second number label and entry
second_number_label = tk.Label(root, text="Second Number")
second_number_label.pack()
second_number_entry = tk.Entry(root)
second_number_entry.pack()
We create a label and an Entry widget for the second number and arrange them on the window using the pack method.
# create the operator label and radiobuttons
operator_label = tk.Label(root, text="Operator")
operator_label.pack()
operator_var = tk.StringVar()
plus_button = tk.Radiobutton(root, text="+", variable=operator_var, value="+")
plus_button.pack()
minus_button = tk.Radiobutton(root, text="-", variable=operator_var, value="-")
minus_button.pack()
times_button = tk.Radiobutton(root, text="*", variable=operator_var, value="*")
times_button.pack()
Following is the entire code in one code block:
import tkinter as tk
# function for performing calculation
def perform_calculation():
# get the value of first and second number
num1 = int(first_number_entry.get())
num2 = int(second_number_entry.get())
# get the operator
operator = operator_var.get()
# perform the calculation
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
result = "Invalid operator"
# display the result
result_label.config(text="Result: " + str(result))
# create the main window
root = tk.Tk()
root.title("Calculator")
root.geometry("300x200")
# create the first number label and entry
first_number_label = tk.Label(root, text="First Number")
first_number_label.pack()
first_number_entry = tk.Entry(root)
first_number_entry.pack()
# create the second number label and entry
second_number_label = tk.Label(root, text="Second Number")
second_number_label.pack()
second_number_entry = tk.Entry(root)
second_number_entry.pack()
# create the operator label and radiobuttons
operator_label = tk.Label(root, text="Operator")
operator_label.pack()
operator_var = tk.StringVar()
plus_button = tk.Radiobutton(root, text="+", variable=operator_var, value="+")
plus_button.pack()
minus_button = tk.Radiobutton(root, text="-", variable=operator_var, value="-")
minus_button.pack()
times_button = tk.Radiobutton(root, text="*", variable=operator_var, value="*")
times_button.pack()
divide_button = tk.Radiobutton(root, text="/", variable=operator_var, value="/")
divide_button.pack()
# create the calculate button
calculate_button = tk.Button(root, text="Calculate", command=perform_calculation)
calculate_button.pack()
# create the result label
result_label = tk.Label(root, text="")
result_label.pack()
# start the main loop
root.mainloop()