Tkinter Designer

← All templates

Calculator template for tkinter

Open this design in the browser-based designer, rearrange it, and download the Python. Nothing to install, and you don't need an account to try it.

Open in designer →

What this template shows you

  • Laying out a button grid quickly
  • Passing arguments to a command with lambda
  • Updating an Entry as a calculator display

The Python it generates

This is the actual main.py you get when you download it: standard-library tkinter, no framework, yours to edit:

import tkinter as tk
from tkinter import ttk


class Application:
    def __init__(self, root):
        self.root = root
        root.title("Calculator")
        root.geometry("262x330")

        self.display = tk.Entry(root)
        self.display.place(x=20, y=20, width=222, height=40)

        self.button1 = tk.Button(root, text="7", command=lambda: self.on_press("7"))
        self.button1.place(x=20, y=80, width=50, height=50)

        self.button2 = tk.Button(root, text="8", command=lambda: self.on_press("8"))
        self.button2.place(x=78, y=80, width=50, height=50)

        self.button3 = tk.Button(root, text="9", command=lambda: self.on_press("9"))
        self.button3.place(x=136, y=80, width=50, height=50)

        self.button4 = tk.Button(root, text="/", command=lambda: self.on_press("/"))
        self.button4.place(x=194, y=80, width=50, height=50)

        self.button5 = tk.Button(root, text="4", command=lambda: self.on_press("4"))
        self.button5.place(x=20, y=138, width=50, height=50)

        self.button6 = tk.Button(root, text="5", command=lambda: self.on_press("5"))
        self.button6.place(x=78, y=138, width=50, height=50)

        self.button7 = tk.Button(root, text="6", command=lambda: self.on_press("6"))
        self.button7.place(x=136, y=138, width=50, height=50)

        self.button8 = tk.Button(root, text="*", command=lambda: self.on_press("*"))
        self.button8.place(x=194, y=138, width=50, height=50)

        self.button9 = tk.Button(root, text="1", command=lambda: self.on_press("1"))
        self.button9.place(x=20, y=196, width=50, height=50)

        self.button10 = tk.Button(root, text="2", command=lambda: self.on_press("2"))
        self.button10.place(x=78, y=196, width=50, height=50)

        self.button11 = tk.Button(root, text="3", command=lambda: self.on_press("3"))
        self.button11.place(x=136, y=196, width=50, height=50)

        self.button12 = tk.Button(root, text="-", command=lambda: self.on_press("-"))
        self.button12.place(x=194, y=196, width=50, height=50)

        self.button13 = tk.Button(root, text="C", command=self.on_clear)
        self.button13.place(x=20, y=254, width=50, height=50)

        self.button14 = tk.Button(root, text="0", command=lambda: self.on_press("0"))
        self.button14.place(x=78, y=254, width=50, height=50)

        self.button15 = tk.Button(root, text="=", command=self.on_equals)
        self.button15.place(x=136, y=254, width=50, height=50)

        self.button16 = tk.Button(root, text="+", command=lambda: self.on_press("+"))
        self.button16.place(x=194, y=254, width=50, height=50)

    def on_press(self, char):
        self.display.insert("end", char)

    def on_clear(self):
        self.display.delete(0, "end")

    def on_equals(self):
        try:
            result = eval(self.display.get())
        except Exception:
            result = "Error"
        self.display.delete(0, "end")
        self.display.insert(0, str(result))


if __name__ == "__main__":
    root = tk.Tk()
    app = Application(root)
    root.mainloop()

Save it and run it the usual way:

python main.py

Make it your own. Edit the layout visually, then download the code.

Open the calculator template →

Other templates

  • Login form: Username + password entries with a working submit handler.
  • To-do list: Entry, listbox, and add/remove buttons, all wired up.
  • Temperature converter: The classic beginner project: Celsius in, Fahrenheit out.
  • Quiz app: Multiple-choice question with radiobuttons and answer checking.