Published using Google Docs
Tkinter Calculator
Updated automatically every 5 minutes

Make a Tkinter Calculator: From Site https://www.educba.com/tkinter-calculator/ 

Tkinter calculator is a simple Graphical User Interface calculator developed by using the Tkinter 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 on both Linux and Windows platforms. In this article, we will see how to develop a simple GUI calculator using the Tkinter module.

Prerequisites for developing Tkinter Calculator:

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

How to Create a simple GUI Tkinter Calculator?

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

What can we do with Tkinter Calculator?

We can perform the following basic arithmetic operations.

Steps to Create Tkinter Calculator

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

# Python code for creating a GUI Tkinter 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 at 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()

Conclusion

In this article, we have discussed how to create a simple GUI Tkinter calculator and perform simple arithmetic operations in it using various examples. The Tkinter module is considered to be very important in python since it is very easy to create many GUI applications. On a Linux or Ubuntu machine you need to download Tkinter 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, 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