Tkinter advanced: Minicalculator on Repl.it

A re-coding of the minicalculator app I made some months ago. Now the script is easier to read and I get rid of some code that wasn’t necessary. So, here is my little calculator that you can also try online on repl.it.

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("MiniCalculator")
    self.pack_entry() 
    self.bind()

  def pack_entry(self):
    "Display the entry widgets for the digit input"
    self.display = StringVar()
    entry = Entry(self, relief=FLAT, textvariable=self.display, justify='right', bd=15, bg='orange')
    entry.pack(side=TOP)
    entry.focus()

  def bind(self):
    "What happens when you press Enter, Delete or BackSpace"
    self.master.bind(
      "<Return>", lambda e: self.calc(self.display))
    self.master.bind(
      "<Delete>", lambda e: self.display.set(""))
    self.master.bind(
      "<BackSpace>", lambda e: self.display.set(""))

  def calc(self, display):
    "Takes what is in the entry, evaluete it and put the result in the entry"
    try:
        display.set(eval(display.get()))
    except:
        display.set("ERROR")


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

In this calculator you put the digit with the keyboard. Press Enter to see the result.

You can find the code and make it run (with a window in the browser!) with Repl.it.

https://repl.it/@EducationalChan/Minicalculator

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.