0% found this document useful (0 votes)
30 views1 page

Tkinter Calculator

This document contains a Python script that implements a simple calculator using the Tkinter library. The calculator can perform addition, subtraction, multiplication, and division, with error handling for invalid inputs and division by zero. The GUI consists of input fields for two numbers and buttons for each operation.

Uploaded by

ilguzefe82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views1 page

Tkinter Calculator

This document contains a Python script that implements a simple calculator using the Tkinter library. The calculator can perform addition, subtraction, multiplication, and division, with error handling for invalid inputs and division by zero. The GUI consists of input fields for two numbers and buttons for each operation.

Uploaded by

ilguzefe82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import tkinter as tk

from tkinter import messagebox

def add():
try:
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 + num2
messagebox.showinfo("Result", f"The result is: {result}")
except ValueError:
messagebox.showerror("Invalid input", "Please enter valid numbers")

def subtract():
try:
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 - num2
messagebox.showinfo("Result", f"The result is: {result}")
except ValueError:
messagebox.showerror("Invalid input", "Please enter valid numbers")

def multiply():
try:
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 * num2
messagebox.showinfo("Result", f"The result is: {result}")
except ValueError:
messagebox.showerror("Invalid input", "Please enter valid numbers")

def divide():
try:
num1 = float(entry1.get())
num2 = float(entry2.get())
if num2 == 0:
messagebox.showerror("Error", "Cannot divide by zero")
else:
result = num1 / num2
messagebox.showinfo("Result", f"The result is: {result}")
except ValueError:
messagebox.showerror("Invalid input", "Please enter valid numbers")

root = tk.Tk()
root.title("Simple Calculator")

tk.Label(root, text="First Number:").grid(row=0, column=0)


entry1 = tk.Entry(root)
entry1.grid(row=0, column=1)

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


entry2 = tk.Entry(root)
entry2.grid(row=1, column=1)

tk.Button(root, text="+", command=add).grid(row=2, column=0)


tk.Button(root, text="-", command=subtract).grid(row=2, column=1)
tk.Button(root, text="*", command=multiply).grid(row=2, column=2)
tk.Button(root, text="/", command=divide).grid(row=2, column=3)

root.mainloop()

You might also like