Final (Report)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

S.H.

JONDHALE POLYTECHNIC, DOMBIVLI (WEST)


ACADEMIC YEAR: 2024-2025
A MICROPROJECT ON
SIMPLE GUI CALCULATOR USING TKINTER
SUBJECT: INDUSTRIAL TRAINING (22057)
GUIDED BY: MR. AKSHAY GAIKWAD
SUBMITTED BY:
1. SEJAL CHANDRAKANT JADHAV
(2200440119)
2. POOJA RAMESH CHAVAN
(2200440122)
3. SHREEYA SANJAY GAIKAR
(2200440129)
4. SAKSHI MADHUKAR SHIROSHE
(23110970250)

UNDER THE GUIDANCE OF PROF. AKSHAY GAIKWAD IN PARTIAL FULFILLMENT


FOR THE AWARD OF THE DIPLOMA IN COMPUTER ENGINEERING
MAHARASHTRA STATEBOARD OF TECHNICAL EDUCATION

1
CERTIFICATE
THIS IS TO CERTIFY THAT SEJAL CHANDRAKANT JADHAV (2200440119) ,
POOJA RAMESH CHAVAN (2200440122), SHREEYA SANJAY GAIKAR
(2200440129) AND SAKSHI MADHUKAR SHIROSHE(23110970250) OF
FIFTH SEMESTER OF DIPLOMA IN COMPUTER ENGINEERING
OF INSTITUTE, S. H. JONDHALE POLYTECHNIC, DOMBIVLI (W)
HAS COMPLETED THE MICRO PROJECT
SATISFACTORILY IN THE SUBJECT INDUSTRIAL TRAINING
FOR THE ACADEMIC YEAR 2024-2025
AS PRESCRIBED IN THE CURRICULUM.

PLACE: DOMBIVLI
DATE:

COURSE TEACHER HEAD OF THE DEPARTMENT PRINCIPAL

2
3
ACKNOWLEDGEMENT

We would like to express our special thanks of


gratitude to our subject teacher mr. Akshay Gaikwad
sir as well as our principal who gave us the opportunity
to do this project on Simple GUI calculator using
tkinter. Which also helped us to do a lot of research
and understanding the fundamentals of python
language . Also, we are really thankful to our parents
and group mates for finalising this project within the
limited time frame.

4
INDEX
TOPIC: SIMPLE GUI CALCULATOR.
SR.NO TOPIC PAGE NO

1 ABSTRACT 6

2 INTRODUCTION 7

3 WHAT IS PYTHON? 9

4 WHAT IS TURTLE GRAPHICS? 10

5 SOFTWARE REQUIREMENTS 11

6 TECHNICAL ASPECTS 13

7 ALGORITHM FOR PROJECT 14

8 EXPLAINATION 16

9 CODE AND OUTPUT 18

10 CONCLUSION 24

5
ABSTRACT
This project presents the implementation of a classic
Creating a simple calculator using tkinter involves setting up a
graphical user interface (GUI) with buttons for numbers and
operations, and handling user input and calculations.
Below is an abstract outline of how you can structure such a project:
1. Import tkinter: Import the tkinter module for creating the GUI :
2. Import everything from tkinter module
3. Function to update expression in the text entry box
4. create a GUI window
5. set the background colour of GUI window
6. Set the title of GUI window
7. Set the configuration of GUI window
8. Create the text entry box for showing the expression
9. Grid method is used for placing the widgets at respective
positions in table like structure
10. Create a Buttons and place at a
particular location inside the root window when user press the
button, the command or function affiliated to that button is
executed
11. Start the GUI using Gui.mainloop()

6
INTRODUCTION
In this project, we're creating a basic calculator using Tkinter, a
Python tool for making windows and buttons. Calculators are
important because they help us do math quickly and accurately.

Our goal is to make a calculator that's easy to use. We'll use Tkinter
to create buttons for numbers and operations like plus and minus.
You can click these buttons or type numbers on your keyboard to use
the calculator.

This report explains how we set up the calculator's look and how we
handle when you click buttons or type on the keyboard. We'll also
show how we made sure the calculator works correctly, even if you
try to divide by zero or type something unexpected.

By showing step-by-step how we built the calculator, this report


helps us understand how to make useful programs with Tkinter in
Python.

7
WHAT IS
PYTHON??
Python is a high-level, interpreted programming language known for its
simplicity and readability. Created by Guido van Rossum and first released in
1991, Python emphasizes code readability and ease of use, making it an
excellent choice for both beginners and experienced developers.
Key Characteristics:
• Simple Syntax:
Python’s syntax is designed to be intuitive and easy to learn, using
indentation to define code blocks, which promotes readable and maintainable
code.
• Interpreted Language:
Python is an interpreted language, meaning that it executes code line by
line. This allows for easier debugging and interactive development.
• High-Level Language:
Python abstracts away many low-level details such as memory
management, allowing developers to focus on writing functional and effective
code.
• Dynamic Typing:
In Python, variable types are determined at runtime, which offers flexibility
in coding but also requires careful handling to avoid runtime errors.
• Extensive Standard Library:

8
Python comes with a rich standard library that supports many common
programming tasks, such as file I/O, system operations, and internet protocols,
reducing the need to write code from scratch.
• Cross-Platform Compatibility:
Python is compatible with various operating systems, including Windows,
macOS, and Linux, ensuring that Python programs can run on different
platforms with minimal changes.
• Versatile and Multi-Paradigm:
Python supports multiple programming paradigms, including procedural,
object-oriented, and functional programming, providing flexibility in how you
write code.

Common Uses
Web Development: Frameworks like Django and Flask are used to build
dynamic web applications.
Data Science and Machine Learning: Libraries such as NumPy, pandas,
TensorFlow, and scikit-learn are popular for data analysis, machine learning,
and scientific computing.
Automation and Scripting: Python is widely used for automating repetitive
tasks and writing scripts to simplify workflows.
Software Development: Used to develop standalone applications, including
games and desktop applications.
Education: Python’s simplicity makes it an excellent language for teaching
programming and computational thinking

9
WHAT IS TURTLE GRAPHICS??
Turtle Graphics is a popular drawing library in Python that provides a simple
way to create graphics and animations. It is inspired by the Logo programming
language, where commands control the movement and drawing of a turtle on
the screen, making it an excellent tool for introducing programming concepts
in a fun and visual way.
Key Features:
• Simple Drawing Commands:
Basic commands like forward(), backward(), left(), and right() control the
turtle's movement, making it straightforward to draw shapes and patterns.
• Pen Control:
Functions like penup(), pendown(), pencolor(), and pensize() allow you to
control the turtle's pen, enabling you to draw with different colors and line
thicknesses.
• Screen Setup:
You can customize the drawing window using commands like setup(),
bgcolor(), and title() to set the size, background color, and title of the window.
• Event Handling:
Turtle Graphics supports event-driven programming with functions like
onclick(), onkey(), and onscreenclick(), allowing you to create interactive
programs.
• Animations:The tracer() and update() functions help manage screen
updates, enabling smooth animations and efficient drawing.

10
SOFTWARE REQUIREMENTS
To create and run Tkinter programs in Python, you'll need the following
software:
1. Python
• Version: Python 3.x (Python 3.6 or later is recommended).
• Installation: Download and install Python from the official Python
website. Ensure you check the box to add Python to your system PATH
during installation.
2. Python IDE or Text Editor
• Integrated Development Environments (IDEs):
o PyCharm: A powerful IDE for Python with extensive features for
development and debugging. Download it from the JetBrains
website.
o Visual Studio Code: A lightweight, open-source code editor with
Python support. Download it from the Visual Studio Code website.
o Thonny: A beginner-friendly Python IDE. Download it from the
Thonny website.
• Text Editors:
o Sublime Text: A sophisticated text editor for code. Download it
from the Sublime Text website.
o Atom: A hackable text editor for the 21st century. Download it
from the Atom website.
3. Tkinter Module
• Included with Python: The Tkinter graphics module is included with the
standard Python library, so you don't need to install it separately.
4. Optional: Package Manager
• pip: Python's package manager pip is used for installing additional
packages or libraries. It usually comes pre-installed with Python, but you
can install it or upgrade it using the command:

11
python -m ensurepip --upgrade
Summary of Installation Steps
1. Install Python: Download and install Python from the official website.
Ensure you add Python to your system PATH during installation.
2. Choose an IDE/Text Editor: Select an IDE or text editor to write and run
your Tkinter GUI code. Install it if needed.
3. Verify Tkinter Module: The Tkinter module comes with Python, so no
additional installation is required.
Running a Turtle Graphics Program
1. Write the Code: Use your chosen IDE or text editor to write your Tkinter
Graphics code.
2. Run the Code: Execute the Python script through your IDE or by running
the script from the command line.
For example, save your Tkinter Graphics code in a file named calculator.py and
run it using:
python calculator.py
This will open a GUI window and execute your program.
By following these steps and using the necessary software, you can easily
create and run Tkinter GUI programs in Python.

12
TECHNICAL ASPECTS
1. Graphical User Interface (GUI) Setup:
o Window Setup: Create a Tkinter window with a specific size and
title.
o Widgets: Use Tkinter widgets like Entry for input display and
Button for numbers, operators, and control functions.
2. Event Handling:
o Button Clicks: Capture button clicks using command callbacks
attached to each button.
o Keyboard Input: Implement key bindings to allow keyboard input
for operations.
3. Calculator Logic:
o Input Handling: Manage user input and display it on the screen.
o Evaluation: Implement a function to evaluate arithmetic
expressions using Python's eval() or custom parsing methods.
4. Error Handling:
o Division by Zero: Handle division by zero errors gracefully.
o Invalid Input: Ensure the calculator can handle unexpected input
and display appropriate messages.
5. User Interaction:
o Feedback: Display results and error messages on the calculator's
screen.
o Clear and Reset: Include functions to clear the current input or
reset the calculator.
6. Code Structure:
o Modular Design: Organize your code into functions and classes for
input handling, evaluation, and UI management.
o Separation of Concerns: Separate UI logic from calculation logic
for better maintainability.
7. Testing:
o Unit Testing: Implement unit tests for core functions like
evaluation and input handling to ensure reliability.

13
Algorithm for Simple GUI Calculator using
Tkinter
1. Initialization
Import necessary modules (tkinter in this case).
Define a global variable expression to store the mathematical expression.
2. GUI Setup:
Create a Tkinter Tk() instance (gui) and configure its properties like background
color, title, and geometry.
Create a StringVar() (equation) to link with the entry field (expression_field)
where the user input and calculated result are displayed.
3. Function Definitions:
Press(num):
Updates the expression by concatenating the pressed button’s value (num).
Updates the equation StringVar to display the updated expression.
Equalpress():
Evaluates the current expression using Python’s eval() function.
Sets the result (total) in the equation StringVar.
Clears the expression for subsequent calculations.
Handles exceptions (like division by zero) using a try-except block and displays
“error” in case of an error.
Clear():
Clears both the expression and the equation StringVar, resetting the calculator.
Button Creation and Placement:
Create buttons for digits (0-9), arithmetic operations (+, -, *, /), decimal point
(.), equals (=), and clear (Clear).
Use lambda functions to associate each button with its respective function
(press for digits and operators, equalpress for equals, clear for clear).

14
4. Grid Layout:
Use the grid() method to place the entry field (expression_field) and all the
buttons in a grid layout within the GUI window (gui).
5. Main Loop:

Start the Tkinter event loop (gui.mainloop()) to display the GUI and handle user
interactions.
6. Example Usage:
User can click buttons to input numbers and arithmetic operations.
Clicking “=” evaluates the expression and displays the result.
Clicking “Clear” resets the calculator.
The GUI updates dynamically as the user interacts with it.
This algorithm outlines the flow of the provided Python code, guiding the
creation and functionality of a basic GUI calculator using Tkinter.

15
EXPLAINATION OF CODE
• The code starts by importing the necessary modules.
• The tkinter module provides all the basic functionality for creating
graphical user interfaces.
• Next, we create a global variable called expression which will store the
result of the calculation.
• We also create two functions to update and evaluate the expression.
• Finally, we write driver code to initialize and manage our GUI window.
• In order to create a simple calculator, we first need to define an
expression variable.
• This is done by using the global keyword and assigning it an empty string
value ( “” ).
• Next, we create two functions to update and evaluate the expression.
• The press function updates the contents of the text entry box while
equalpress evaluates the final result of the calculation.
• We next need to create a table-like structure in which our widgets will
be placed.
• We do this by using grid method which takes three arguments:
columnspan , ipadx , and rowspan .
• These parameters specify how many columns wide, how many rows
high, and how many columns per row respectively should be used in our
table layout.
• We set columnspan to 4 , meaning that there will be four columns in our
table, iPad width divided by 2 (70), multiplied by 1 for each row in our
table (iPad height divided
• The code creates a simple calculator using the Tkinter module.
• First, the code imports everything from the Tkinter module.
• Next, the code creates two global variables: expression and total.
• The press() function is used to update the expression variable in the text
entry box.
• The equalpress() function is used to evaluate the final expression.
• Finally, the clear() function is used to clear the contents of the text entry
box.
• Next, the driver code is created.

16
• In this code, if __name__ == “__main__”: is executed which will create a
GUI window and set its background color to light green and its title to
Simple Calculator.
• Next, the geometry() method is used to set the size of the GUI window
(270
• The code starts with a few basic objects: a Button object, which has
properties for text, font, background color, and command; and a grid
object.
• The first three buttons (button1 through button3) each have their own
individual commands associated with them.
• When the user clicks on one of these buttons, the corresponding
command is executed.
• For example, when the user clicks on button1, its command is to press
the number 1 key.
• Similarly, when the user clicks on button2’s command, it will be to press
the number 2 key; and so on.
• Similarly, when the user clicks on button4’s command (to increase the
value by 1), its grid row and column values will be set to 3 and 0
respectively.
• And finally when clicking on button5’s command (to decrease the value
by 1), its grid row and column values will be set to 2 and 1 respectively.
• The code creates seven buttons, each with its own function.
• When the user presses one of the buttons, the corresponding command
is executed.
• The first button, button1, has the function press(1).
• When clicked, this button will execute the code lambda: press(1).
• The second button, button2, has the function press(2), and so on.
• When all seven buttons have been clicked, their functions will be
executed in order.

17
CODE
# impor t everything from tkinter module
From tkinter import *

# globally declare the expression variable


Expression = “”

# Function to update expression


# in the text entry box
Def press(num):
# point out the global expression variable
Global expression

# concatenation of string
Expression = expression + str(num)

# update the expression by using set method


Equation.set(expression)

#Function to evaluate the final expression


Def equalpress():
# Try and except statement is used
# for handling the errors like zero
# division error etc.

# Put that code inside the try block


# which may generate the error
Try:

Global expression

# eval function evaluate the expression


# and str function convert the result
# into string
Total = str(eval(expression))

Equation.set(total)

# initialize the expression variable

18
# by empty string
Expression = “”

# if error is generate then handle


# by the except block
Except:

Equation.set(“ error “)
Expression = “”

# Function to clear the contents


# of text entry box
Def clear():
Global expression
Expression = “”
Equation.set(“”)

# Driver code
If __name__ == “__main__”:
# create a GUI window
Gui = Tk()

# set the background colour of GUI window


Gui.configure(background=”white”)

# set the title of GUI window


Gui.title(“Simple Calculator”)

# set the configuration of GUI window


Gui.geometry(“270x150”)

# StringVar() is the variable class


# we create an instance of this class
Equation = StringVar()

# create the text entry box for


# showing the expression .
Expression_field = Entry(gui, textvariable=equation)

19
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
Expression_field.grid(columnspan=10, ipadx=200)

# create a Buttons and place at a particular


# location inside the root window .
# when user press the button, the command or
# function affiliated to that button is executed .
Button1 = Button(gui, text=’ 1 ‘, fg=’black’, bg=’grey’,
Command=lambda: press(1), height=7, width=7)
Button1.grid(row=2, column=0)

Button2 = Button(gui, text=’ 2 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(2), height=7, width=7)
Button2.grid(row=2, column=1)

Button3 = Button(gui, text=’ 3 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(3), height=7, width=7)
Button3.grid(row=2, column=2)

Button4 = Button(gui, text=’ 4 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(4), height=7, width=7)
Button4.grid(row=3, column=0)

Button5 = Button(gui, text=’ 5 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(5), height=7, width=7)
Button5.grid(row=3, column=1)

Button6 = Button(gui, text=’ 6 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(6), height=7, width=7)
Button6.grid(row=3, column=2)

Button7 = Button(gui, text=’ 7 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(7), height=7, width=7)
Button7.grid(row=4, column=0)

Button8 = Button(gui, text=’ 8 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(8), height=7, width=7)

20
Button8.grid(row=4, column=1)

Button9 = Button(gui, text=’ 9 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(9), height=7, width=7)
Button9.grid(row=4, column=2)

Button0 = Button(gui, text=’ 0 ‘, fg=’black’, bg=’grey’,


Command=lambda: press(0), height=7, width=7)
Button0.grid(row=5, column=0)

Plus = Button(gui, text=’ + ‘, fg=’black’, bg=’grey’,


Command=lambda: press(“+”), height=7, width=7)
Plus.grid(row=2, column=3)

Minus = Button(gui, text=’ – ‘, fg=’black’, bg=’grey’,


Command=lambda: press(“-“), height=7, width=7)
Minus.grid(row=3, column=3)

Multiply = Button(gui, text=’ * ‘, fg=’black’, bg=’grey’,


Command=lambda: press(“*”), height=7, width=7)
Multiply.grid(row=4, column=3)

Divide = Button(gui, text=’ / ‘, fg=’black’, bg=’grey’,


Command=lambda: press(“/”), height=7, width=7)
Divide.grid(row=5, column=3)

Equal = Button(gui, text=’ = ‘, fg=’black’, bg=’grey’,


Command=equalpress, height=7, width=7)
Equal.grid(row=5, column=2)

Clear = Button(gui, text=’Clear’, fg=’black’, bg=’grey’,


Command=clear, height=7, width=7)
Clear.grid(row=5, column=’1’)

Decimal= Button(gui, text=’.’, fg=’black’, bg=’grey’,


Command=lambda: press(‘.’), height=7, width=7)
Decimal.grid(row=6, column=0)
# start the GUI
Gui.mainloop()

21
OUTPUT:

22
23
CONCLUSION

Building a simple GUI calculator using Tkinter provides a


practical introduction to GUI programming in Python. It
combines user interface design, event-driven
programming, and basic arithmetic operations into a
single application. As you progress, you can enhance the
calculator by adding more features such as scientific
functions or memory operations.

24
REFERENCES
• https://www.geeksforgeeks.org/python-simple-gui-calculator-using-
tkinter/?ref=lbp
• https://chatgpt.com/

25

You might also like