You may think that a calculator is not so important today that we have it on every mobile, you can use spreadsheets etc. We are in a world dominated by computers and mobile phone full of apps… but I think it could always be interesting to have a calculator and a text editor… together. I don’t know if there are other apps like this, but I don’t know any, so I thought that could be a nice thing to add some features to my lastes minicalculator app. This app followed this progression:
- a simple calculator controlled by the keyboard
- a calculator that displays all the operations in a listbox
- (now) a calculator that manipulates the text with the operations and can save them
I think that this option is cool and not so obvious. You ca use a notepad, you can use a calculator, but I do not have an app that makes me do memorize the operation and write notes around them at the same time. I think to a possible use of it while I am doing things on the fly and I want to be able to go back to the note to see what those operations were about. This is the code, followed by the video that explains how I changed the previous code (second item of the list of the progress above) to the new one. Have fun coding!
import tkinter as tk import os # This class inherit from Frame class App(tk.Frame): def __init__(self): tk.Frame.__init__(self) self.option_add("*Font", "arial 20 bold") self.pack(expand=tk.YES, fill=tk.BOTH) self.master.title("Calculator") self.master.tk.call('wm', 'iconphoto', self.master._w, tk.PhotoImage(file='calculator.png')) # the widgets: display, clear button... self._display() self.list_of_ops() def _display(self): display = tk.StringVar() entry = tk.Entry( self, relief=tk.FLAT, textvariable=display, justify='right', bd=15, bg='orange') entry.pack(side=tk.TOP) entry.focus() entry.bind("<Return>", lambda x: self.calc(display)) entry.bind("<Escape>", lambda x: display.set("")) self.master.bind("<Control-s>", self.save) def save(self, event): "Save the memo about the operations" with open("memo.txt", "w", encoding="utf-8") as file: file.write(self.text.get("0.0", tk.END)) os.startfile("memo.txt") def list_of_ops(self): self.text = tk.Text(self, height=10, width=30) self.text.pack(fill="both", expand=1) def calc(self, display): try: ris = eval(display.get()) if str(display.get()) == str(ris): display.set("") else: self.text.insert(tk.END, display.get() + "=" + str(ris) + "\n") display.set(ris) except NameError as e: display.set("ERR: press ESC") if __name__ == '__main__': a = App() a.mainloop()
Live coding video calculator app
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame
Videos
Speech recognition gamePygame's Platform Game
Other Pygame's posts