0% found this document useful (0 votes)
22 views

Ai Ppt Calculator

The document describes a Python script for creating a simple command-line and GUI calculator using the tkinter library. It supports basic arithmetic operations and includes functionalities like backspace and clear, while handling errors such as division by zero with a popup message. The layout is organized using a grid system, and button commands are implemented with lambda functions.

Uploaded by

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

Ai Ppt Calculator

The document describes a Python script for creating a simple command-line and GUI calculator using the tkinter library. It supports basic arithmetic operations and includes functionalities like backspace and clear, while handling errors such as division by zero with a popup message. The layout is organized using a grid system, and button commands are implemented with lambda functions.

Uploaded by

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

ARTIFICAL INTELLIGENCE

HOLIDAY HOMEWORK
PYTHON CODE EXECUTION: SIMPLE
CALCULATOR
Creating a calculator in Python is a
great way to practice
programming.

It is a detailed Python script that


creates a simple command-line
calculator. This calculator can
perform basic arithmetic
operations: addition, subtraction,
multiplication, and division.
This Python code creates a
graphical user interface (GUI) for a
simple calculator using the tkinter
library.

The calculator supports basic


arithmetic operations, including
addition, subtraction,
multiplication, and division, along
with additional functionalities such
as clearing the input and handling
backspace.
DETAILED BREAKDOWN OF THE
 IMPORT STATEMENTS
CODE
Tk: Main window class for creating the GUI.
END: Constant used to specify the end of the text in the Entry widget.
Entry: Widget for single-line text input.
N, E, S, W: Constants used for widget placement (north, east, south, west).
Button: Widget for clickable buttons.
font: Module for setting font styles.
Label: Widget for displaying text.
partial: Function from functools used to fix a certain number of arguments of
a function and generate a new function.

 FUNCTION DEFINITIONS

get_input(entry, argu)
Inserts the argu string into the Entry widget at the end of
its current content. This is used to append numbers and
operators to the input field.
backspace(entry)
Deletes the last character of the input in the Entry
widget, simulating a backspace function.

clear(entry)
Clears all text from the Entry widget.

calc(entry)
Evaluates the mathematical expression entered in the
Entry widget using eval(). If there's a division by zero, it
calls popupmsg() to show an error message. After
computation, it clears the input field and displays the
result.

popupmsg()
Creates a small popup window that displays an error
message when division by zero is attempted. Includes
an "Okay" button to close the popup.
Cal()
Creates the main application window with the title "Calc" and
disables resizing.

Entry Widget Setup


Creates an Entry widget for displaying and inputting text. It
uses a font size of 15 and is right-aligned. It spans across 4
columns and is positioned at the top of the grid.

Button Styling
Defines colors for different button types.

Button Creation
Uses partial to create a version of the Button class with pre-
defined styles for number buttons and calculation buttons

Creating and Positioning Buttons


Each button is created with a specific label and command. The
command parameter is a lambda function that calls get_input()
with the appropriate value.
Special Buttons
Special buttons for backspace, clear,
equals, and exponentiation.

MAIN EXECUTION
Ensures that the cal() function is called to run the application only when the script is
executed directly, not when imported as a module.

SUMMARY
The code sets up a simple calculator GUI using tkinter. It provides
basic arithmetic operations and functionalities like backspace and
clear. It uses eval() to evaluate mathematical expressions and
handles errors such as division by zero with a popup message. The
GUI layout is created using the grid system, and button commands
are assigned using lambda functions.

You might also like