Tkinter Smallest Calculator ever

This is the smallest calculator you can get… with tkinter module to make a grafic user interface (very minimal, indeed).

from tkinter import *

# This class inherit from Frame
class App(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.option_add("*Font", "arial 20 bold")
        self.pack(expand=YES, fill=BOTH)
        self.master.title("Calculator")
        # the widgets: display, clear button...
        self._display()

    def _display(self):
        display = StringVar()
        entry = Entry(
            self,
            relief=FLAT,
            textvariable=display,
            justify='right',
            bd=15,
            bg='orange')
        entry.pack(side=TOP)
        entry.focus()
        self.master.bind("<Return>", lambda x: self.calc(display))
        self.master.bind("<Escape>", lambda x: display.set(""))


    def calc(self, display):
        try:
            ris = eval(display.get())
            if str(display.get()) == str(ris):
                display.set("")
            else:
                display.set(ris)
        except NameError as e:
            display.set("ERR: press ESC")


if __name__ == '__main__':
    App().mainloop()

This is what it looks like, and do not say I didn’t warned you


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.