Open In App

Simple GUI calculator using Tkinter-Python

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.

To create a Tkinter:

  1. Importing the module – tkinter
  2. Create the main window (container)
  3. Add any number of widgets to the main window
  4. Apply the event Trigger on the widgets.

Below is what the GUI looks like:

GUI
GUI

Let's create a GUI-based simple calculator using the Python Tkinter module, which can perform basic arithmetic operations addition, subtraction, multiplication, and division.

Below is the implementation: 

Python
from tkinter import *
expr = ""  # Global expression string

def press(key):
    global expr
    expr += str(key)
    display.set(expr)

def equal():
    global expr
    try:
        result = str(eval(expr))
        display.set(result)
        expr = ""
    except:
        display.set("error")
        expr = ""

def clear():
    global expr
    expr = ""
    display.set("")

if __name__ == "__main__":
    root = Tk()
    root.configure(bg="light green")
    root.title("Simple Calculator")
    root.geometry("270x150")

    display = StringVar()
    entry = Entry(root, textvariable=display)
    entry.grid(columnspan=4, ipadx=70)

    # Number buttons
    btn1 = Button(root, text='1', fg='black', bg='red', command=lambda: press(1), height=1, width=7)
    btn1.grid(row=2, column=0)
    btn2 = Button(root, text='2', fg='black', bg='red', command=lambda: press(2), height=1, width=7)
    btn2.grid(row=2, column=1)
    btn3 = Button(root, text='3', fg='black', bg='red', command=lambda: press(3), height=1, width=7)
    btn3.grid(row=2, column=2)
    btn4 = Button(root, text='4', fg='black', bg='red', command=lambda: press(4), height=1, width=7)
    btn4.grid(row=3, column=0)
    btn5 = Button(root, text='5', fg='black', bg='red', command=lambda: press(5), height=1, width=7)
    btn5.grid(row=3, column=1)
    btn6 = Button(root, text='6', fg='black', bg='red', command=lambda: press(6), height=1, width=7)
    btn6.grid(row=3, column=2)
    btn7 = Button(root, text='7', fg='black', bg='red', command=lambda: press(7), height=1, width=7)
    btn7.grid(row=4, column=0)
    btn8 = Button(root, text='8', fg='black', bg='red', command=lambda: press(8), height=1, width=7)
    btn8.grid(row=4, column=1)
    btn9 = Button(root, text='9', fg='black', bg='red', command=lambda: press(9), height=1, width=7)
    btn9.grid(row=4, column=2)
    btn0 = Button(root, text='0', fg='black', bg='red', command=lambda: press(0), height=1, width=7)
    btn0.grid(row=5, column=0)

    # Operator buttons
    plus = Button(root, text='+', fg='black', bg='red', command=lambda: press('+'), height=1, width=7)
    plus.grid(row=2, column=3)
    minus = Button(root, text='-', fg='black', bg='red', command=lambda: press('-'), height=1, width=7)
    minus.grid(row=3, column=3)
    mult = Button(root, text='*', fg='black', bg='red', command=lambda: press('*'), height=1, width=7)
    mult.grid(row=4, column=3)
    div = Button(root, text='/', fg='black', bg='red', command=lambda: press('/'), height=1, width=7)
    div.grid(row=5, column=3)

    # Other buttons
    eq = Button(root, text='=', fg='black', bg='red', command=equal, height=1, width=7)
    eq.grid(row=5, column=2)
    clr = Button(root, text='Clear', fg='black', bg='red', command=clear, height=1, width=7)
    clr.grid(row=5, column=1)
    dot = Button(root, text='.', fg='black', bg='red', command=lambda: press('.'), height=1, width=7)
    dot.grid(row=6, column=0)

    root.mainloop()

Output

Explanation:

  • press(key) appends input to expr and updates the display, equal() evaluates expr and shows result or error, clear() resets everything.
  • Sets up the main window with title, size, background and a StringVar display linked to an entry widget for showing input/output.
  • Numeric buttons (0–9) arranged in a grid, each calls press() with its number.
  • Operator buttons (+, -, *, /) placed in the grid, each calls press() with the operator symbol.
  • Additional buttons: = triggers calculation, Clear resets and . adds a decimal point.
  • Uses grid() layout for neat positioning buttons have uniform size and color, runs the Tkinter main event loop.

Next Article
Article Tags :
Practice Tags :

Similar Reads