0% found this document useful (0 votes)
8 views2 pages

(Py Exp9

This document contains a Python script that creates a simple calculator using the Tkinter library. It includes functions for button clicks, clearing the entry, and calculating results based on user input. The calculator has a graphical user interface with buttons for digits and operations, and it handles basic arithmetic operations.

Uploaded by

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

(Py Exp9

This document contains a Python script that creates a simple calculator using the Tkinter library. It includes functions for button clicks, clearing the entry, and calculating results based on user input. The calculator has a graphical user interface with buttons for digits and operations, and it handles basic arithmetic operations.

Uploaded by

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

import tkinter as tk

def on_button_click(value):
current_text = [Link]()
[Link](0, [Link])
[Link]([Link], current_text + value)

def clear_entry():
[Link](0, [Link])

def calculate_result():
try:
expression = [Link]()
result = eval(expression)
[Link](0, [Link])
[Link]([Link], str(result))
except Exception as e:
[Link](0, [Link])
[Link]([Link], "Error")

# Create the main window root = [Link]() [Link]("Simple Calculator")

# Entry widget for displaying input and results entry = [Link](root, width=20, font=('Arial', 14))
[Link](row=0, column=0, columnspan=4)

# Buttons for digits and operations buttons = [


('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('C', 4, 1), ('=', 4, 2), ('+', 4, 3)
]

for (text, row, col) in buttons:


button = [Link](root, text=text, width=5, height=2, command=lambda t=text:
on_button_click(t))

[Link](row=row, column=col)

# Event for the clear button [Link]('<Escape>', lambda event: clear_entry())

# Event for the equals button [Link]('<Return>', lambda event: calculate_result())

# Set the window size [Link]("300x300")

# Run the main loop [Link]()

OUTPUT:

You might also like