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.
Nesta página do site você pode assistir ao vídeo on-line calculator using python gui duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Daily Tutorials 20 Janeiro 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 3,756 vezes e gostou 89 espectadores. Boa visualização!