Calculator… not just like you imagine it
I want to show a simple use of tkinter, very simple, but with a practical example that can be useful for many simple tasks, that you can make more complex for your purposes.
The code for the calculator
Here is the code, no buttons as you can use the keyboard to do all the things you need.
from tkinter import * #This function returns a Frame def iCalc(source, side): "Returns a Frame object yet packed and expanded, to shorten the code" # the bd is the border of the frame storeObj = Frame(source, borderwidth=10, bd=1, bg="gray") # the pack methos is needed to diplay the object storeObj.pack(side=side, expand=YES, fill=BOTH) return storeObj # 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") # THE DISPLAY display = StringVar() # relief can be FLAT or RIDGE or RAISED or SUNKEN GROOVE entry = Entry(self, relief=FLAT, textvariable=display, justify='right', bd=15, bg='orange') entry.pack(side=TOP) # I added an action to calculate the operation when Return (Enter) is hit entry.focus() # You can hit enter to get the result instead of clicking = self.master.bind("<Return>", lambda e, s=self, storeObj=display: s.calc(storeObj)) # YOu can click del button on the keyboard to cancel self.master.bind("<Delete>", lambda e, s=self, storeObj=display: storeObj.set("")) self.master.bind("<BackSpace>", lambda e, s=self, storeObj=display: storeObj.set("")) def calc(self, display): try: # Sets the display to the evaluation of the string in the display itself, i.e. calculate the result display.set(eval(display.get())) except: # if something goes wrong with the result display.set("ERROR") if __name__ == '__main__': App().mainloop()
Make an exe from the script minicalculator
Read this article to know how to do it, or write this on cmd, after you installed pyinstaller with
pip install pyinstaller
pyinstaller --noconsole --onefile --noupx minicalculator.py
You will find the exe file in the ‘dist‘ folder that will appear in the folder of the .py file.
If you double click on the exe file and it does not run, open the cmd in that folder and write
.\minicalculator
You can also try this: right click on the exe file and choose ‘run as administrator‘. The next time you will not need to do this, you can just double click on it.
Utilities