calculator using python gui

Published: 20 January 2023
on channel: Daily Tutorials
3,756
89

Creating a calculator using a GUI in Python can be done using the Tkinter library. Here is an example of a basic calculator that can perform addition, subtraction, multiplication, and division

import tkinter as tk

def calculate():
first_number = float(first_number_entry.get())
second_number = float(second_number_entry.get())
operation = operator.get()

if operation == "+":
result = first_number + second_number
elif operation == "-":
result = first_number - second_number
elif operation == "*":
result = first_number * second_number
elif operation == "/":
result = first_number / second_number
else:
result = "Invalid Operation"

result_label.config(text="Result: " + str(result))

app = tk.Tk()
app.title("Calculator")

first_number_label = tk.Label(app, text="First Number")
first_number_label.grid(row=0, column=0)

first_number_entry = tk.Entry(app)
first_number_entry.grid(row=0, column=1)

second_number_label = tk.Label(app, text="Second Number")
second_number_label.grid(row=1, column=0)

second_number_entry = tk.Entry(app)
second_number_entry.grid(row=1, column=1)

operator = tk.StringVar()
operator.set("+")

addition_button = tk.Radiobutton(app, text="+", variable=operator, value="+")
addition_button.grid(row=2, column=0)

subtraction_button = tk.Radiobutton(app, text="-", variable=operator, value="-")
subtraction_button.grid(row=2, column=1)

multiplication_button = tk.Radiobutton(app, text="*", variable=operator, value="*")
multiplication_button.grid(row=3, column=0)

division_button = tk.Radiobutton(app, text="/", variable=operator, value="/")
division_button.grid(row=3, column=1)

calculate_button = tk.Button(app, text="Calculate", command=calculate)
calculate_button.grid(row=4, column=0, columnspan=2, pady=10)

result_label = tk.Label(app, text="Result:")
result_label.grid(row=5, column=0, columnspan=2)

app.mainloop()



This creates a simple calculator that allows user to input two numbers, select an operation and then get the result. It uses Tkinter to create the GUI elements, such as labels, entry fields, and buttons. The calculate() function performs the calculation based on the selected operation and the input numbers, and the result is displayed in the result label.


On this page of the site you can watch the video online calculator using python gui with a duration of hours minute second in good quality, which was uploaded by the user Daily Tutorials 20 January 2023, share the link with friends and acquaintances, this video has already been watched 3,756 times on youtube and it was liked by 89 viewers. Enjoy your viewing!