Simple Calculator with Tkinter
Introduction
This document describes the implementation of a basic calculator using the Tkinter library in Python. The calculator supports basic arithmetic operations: addition, subtraction, multiplication, and division. It features a graphical user interface (GUI) with buttons for each digit, arithmetic operators, and a clear button.
Code Overview
The code provided creates a simple calculator application with the following functionalities:
Number Entry: Buttons for digits 0-9.
Operators: Buttons for addition (
+
), subtraction (-
), multiplication (*
), and division (/
).Special Buttons: An equals button (
=
) to perform calculations and a clear button to reset the calculator.
Detailed Explanation
Importing Tkinter
from tkinter import *
The tkinter
library is imported to create the graphical user interface (GUI) for the calculator.
Initializing the Main Window
window = Tk()
window.title("Simple Calculator")
window.geometry('500x330')
Tk()
initializes the main window of the application.window.title("Simple Calculator")
sets the window title.window.geometry('500x330')
defines the size of the window.
Global Variables
n1 = 0
n2 = 0
op = None
res = StringVar()
n1
andn2
store the numbers used in calculations.op
stores the current operator.res
is aStringVar
used to display results and updates in the GUI.
Functions
getvalue(a)
: Updates the number displayed based on button clicks.def getvalue(a): global n1, n2, res if op is None: number = n1 * 10 + a n1 = number else: number = n2 * 10 + a n2 = number res.set(str(number))
If no operator is selected, the function updates
n1
.If an operator is selected, it updates
n2
.
getop(a)
: Sets the operator for the calculation.def getop(a): global op op = a res.set(str(op))
- Sets the operator (
+
,-
,*
,/
) and updates the display.
- Sets the operator (
clears()
: Resets the calculator.def clears(): global n1, n2, op n1 = 0 n2 = 0 op = None res.set('0')
- Resets
n1
,n2
, andop
to their initial values and updates the display to0
.
- Resets
calculate()
: Performs the calculation based on the selected operator.def calculate(): if op == '-': res.set(str(n1 - n2)) elif op == '+': res.set(str(n1 + n2)) elif op == '/': res.set(str(n1 / n2)) else: res.set(str(n1 * n2))
- Performs the arithmetic operation based on
op
and updates the result.
- Performs the arithmetic operation based on
GUI Elements
Display Area
msg = Entry(window, bd=20, textvariable=res, relief='sunken', font=("Helvetica", 20)) msg.grid(columnspan=4, ipadx=80, ipady=10)
Entry
widget used to display the current value or result.textvariable=res
binds the display to theres
variable.
Buttons
Buttons are created for digits, operators, and special functions.
button7 = Button(text=7, width=15, height="2", bg='lightgrey', command=lambda: getvalue(7)) button7.grid(row=1, column=0, pady=1)
Button
widget for digit7
withcommand=lambda: getvalue(7)
ensures thegetvalue
function is called with7
as an argument.
Similar button creation is done for other digits, operators, and special functions like clear and equals.
Main Loop
mainloop()
- The
mainloop()
function starts the Tkinter event loop, making the application responsive to user interactions.
Summary
The provided code creates a functional GUI calculator using Tkinter. It handles basic arithmetic operations and provides a user-friendly interface for performing calculations. The use of Tkinter widgets such as Entry
and Button
allows for straightforward interaction and display of results.