EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter Calculator

Tkinter Calculator

Balaji S
Article byBalaji S
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 30, 2023

Tkinter Calculator

Definition of Tkinter Calculator

Tkinter calculator is a simple Graphical User Interface calculator developed by using the module. Python offers many ways for developing various GUI applications. Tkinter module is one of the most useful and easiest ways for developing GUI applications because Tkinter works well in both Linux and Windows platforms. This article will show how to develop a simple GUI calculator using the Tkinter module.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Prerequisites for developing:

In order to create a simple Graphical User Interface calculator using Tkinter we need the following prerequisites.

  • Tkinter
  • Lamda

How to Create a Simple GUI Calculator?

In order to create a simple GUI do the following steps.

  • Import the Tkinter module
  • create the main user interface. (window for our calculator)
  • Add the widgets to the user interface
  • Apply the event trigger for the widgets

What can we do with Tkinter Calculator?

We can perform the following basic arithmetic operations.

  • Addition
  • Subtraction
  • Division
  • Multiplication

Steps to Create

Let us see how to create a GUI calculator step by step

  • Create the GUI Interface

# Python code for creating a GUI calculator.

# import tkinter package
from tkinter import *
# create a GUI user interface
if __name__ == "__main__":
giu_face = Tk()

# set the background color for the GUI interface.

giu_face.configure(background="red")
# set the title for GUI interface
giu_face.title("Simple Calculator")

# set the size of the GUI window interface.

giu_face.geometry("370x180")
giu_face.mainloop()
exp = ""
  • Function to Update the exp in the Text Entry Box

def pres(number):
global exp
# string concatenation
exp = exp + str(number)
# using set method to update the expression
equation.set(exp)
  • Code for Calculating Ultimate Expression

def pres():
try:
global exp

  • Using Eval Function to Calculate the Exp given and Convert the Result to String
# using eval function to calculate expression
total_value = str(eval(exp))
equation.set(total_value)
exp = ""
using except block to handle errors if generated
# using except block to handle generated errors
except:
equation.set(" error ")
exp = ""
  • Function for Clearing the Contents of the Calculator
def clearng():
global exp
exp = ""
equation.set("")
equation = StrVar()
  • Function for Creating the EntryBox for Typing the Text for Operation
exp_field = Ent(gui, text_variable=equation)
  • Using the Grid Method for Assigning the Widgets to their respective positions.
exp_field.grid(columnspan = 5)
equation.set('enter your value’)
  • Creating the Buttons for Typing the Numbers

# create the Buttons and position them inside the window.

bton1 = Button(giu_face, text =' 1 ', font="lucida 20 bold",
command = lambda: input_val(1), height = 1, width = 7)
bton1.grid(row = 2, column = 0, ipady = 4 , ipadx = 2)
bton2 = Button(giu_face, text = ' 2 ', font="lucida 20 bold",
command = lambda: input_val(2), height = 1, width = 7)
bton2.grid(row = 2, column = 1, ipady = 4 , ipadx = 2)
bton3 = Button(giu_face, text = ' 3 ', font="lucida 20 bold",
command = lambda: input_val(3), height = 1, width = 7)
bton3.grid(row = 2, column = 2, ipady = 4 , ipadx = 2)
bton4 = Button(giu_face, text = ' 4 ', font="lucida 20 bold",
command = lambda: input_val(4), height = 1, width = 7)
bton4.grid(row = 3, column = 0, ipady = 4 , ipadx = 2)
bton5 = Button(giu_face, text = ' 5 ', font="lucida 20 bold",
command = lambda: input_val(5), height = 1, width = 7)
bton5.grid(row = 3, column = 1, ipady = 4 , ipadx = 2)
bton6 = Button(giu_face, text = ' 6 ', font="lucida 20 bold",
command = lambda: input_val(6), height = 1, width = 7)
bton6.grid(row = 3, column = 2, ipady = 4 , ipadx = 2)
bton7 = Button(giu_face, text = ' 7 ', font="lucida 20 bold",,
command = lambda: input_val(7), height = 1, width = 7)
bton7.grid(row = 4, column = 0, ipady = 4 , ipadx = 2)
bton8 = Button(giu_face, text = ' 8 ', font="lucida 20 bold",
command = lambda: input_val(8), height = 1, width = 7)
bton8.grid(row = 4, column = 1, ipady = 4 , ipadx = 2)
bton9 = Button(giu_face, text = ' 9 ', font="lucida 20 bold",
command = lambda: input_val(9), height = 1, width = 7)
bton9.grid(row = 4, column = 2, ipady = 4 , ipadx = 2)
bton10 = Button(giu_face, text = ' 0 ', font="lucida 20 bold",
command = lambda: input_val(0), height = 1, width = 7)
bton10.grid(row = 5, column = 0, ipady = 4 , ipadx = 2)
plus = Button(giu_face, text=' + ', font="lucida 20 bold",
command = lambda: input_val("+"), height=1, width=7)
plus.grid(row = 2, column = 3, ipady = 4 , ipadx = 2)
minus = Button(giu_face, text = ' - ', font="lucida 20 bold",
command = lambda: input_val("-"), height = 1, width = 7)
minus.grid(row = 3, column = 3, ipady = 4 , ipadx = 2)
multiply = Button(giu_face, text = ' * ', font="lucida 20 bold",
command = lambda: input_val("*"), height = 1, width = 7)
multiply.grid(row = 4, column = 3, ipady = 4 , ipadx = 2)
divide = Button(giu_face, text=' / ', font="lucida 20 bold",
command = lambda: input_val("/"), height = 1, width = 7)
divide.grid(row = 5, column = 3, ipady = 4 , ipadx = 2)
equal = Button(giu_face, text=' = ', font="lucida 20 bold",
command = input_val, height = 1, width = 7)
equal.grid(row = 5, column = 2, ipady = 4 , ipadx = 2)
clearng = Button(giu_face, text = 'clearng', font="lucida 20 bold",
command = clearng, height = 1, width = 7)
clearng.grid(row = 5, column = '1', ipady = 4 , ipadx = 2)
Decimal = Button(giu_face, text='.', font="lucida 20 bold",
command = lambda: input_val('.'), height = 1, width = 7)
Decimal.grid(row = 6, column = 0, ipady = 4 , ipadx = 2)
giu_face.mainloop()

Output:

Tkinter Calculator

Conclusion

In this article, we discussed how to create a simple GUI Tkinter calculator and perform simple arithmetic operations using various examples. The module is considered to be very important in Python since it is very easy to create many GUI applications. In Linux or Ubuntu machines, you need to download separately and install it. But in Windows OS, it is readily available in the Anaconda package. We hope this article helps. Thank you.

Recommended Articles

This is a guide to Tkinter Calculator. Here we discuss the Definition of, How to Create a simple GUI Tkinter Calculator? with sample code for better understanding. You may also have a look at the following articles to learn more –

  1. Tkinter Notebook
  2. Tkinter PhotoImage
  3. Tkinter Combobox
  4. Tkinter Font
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more