This free app let you do some math operations and also gives you the chance to save them easily on a text file that you can save as a txt, a md and an html file. You can use markdow (# Title is equivalent to <h1>Title</h1>) for example to save then the text into html pressing ctrl + b. With ctrl + s you save it as a text.
This time I added to my CalculatorMemo 1.0 app these features:
- save file to md and html
- use markdown into text
- advise for error in case you put a 0 before a number (not allowed)
Here is the code that I obtained mixing the md_html.py script that I showed you in this post, with the code for the calculator memo.
From this mix, I can not show the operation in a text box, modify the text, use markdown into the text and create an html file pressing control + b. I think it’s handy for many situations that I’ll let you imagine (a sketch, an html tutorial for your web page, a presentation in html… name it). Thinking of wich, I am thinking to make a new crossover among script, using that code I made to create slides in html with Python. This will be done in the 1.1 version of this app. Until then, this is the code and here you can find the repository.
You may need to install:
- pip install markdown jinja2
import tkinter as tk import os from datetime import datetime import markdown import contextlib TEMPLATE = """<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="referrer" content="no-referrer" /> <meta name="referrer" content="unsafe-url" /> <meta name="referrer" content="origin" /> <meta name="referrer" content="no-referrer-when-downgrade" /> <meta name="referrer" content="origin-when-cross-origin" /> <title>Page Title</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style> body { font-family: Helvetica,Arial,sans-serif; } code, pre { font-family: monospace; } </style> </head> <body> <div class="container"> {{content}} </div> </body> </html> """ def main(args=None): with open(args) as file: md = file.read() extensions = ['extra', 'smarty'] html = markdown.markdown(md, extensions=extensions, output_format='html5') doc = TEMPLATE.replace('{{content}}', html); with open("file.html", "w") as file: file.write(doc) os.startfile("file.html") # 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 ctrl+s=save txt ctrl+b=save md") 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) self.master.bind("<Control-b>", self.start) def getname(self): d = datetime.today() d = str(d).split(":") d = "".join(d) d = d.replace("-","_") d = d.replace(" ","_") d = d.replace(".","_") name = "memo" + d + ".txt" return name def save(self, event): "Save the memo about the operations" name = self.getname() with open(name, "w", encoding="utf-8") as file: file.write(self.text.get("0.0", tk.END)) os.startfile(name) def start(self, event): name = self.getname() testo = self.text.get("0.0", tk.END) with open("file_1.md", "w") as file: file.write(testo) # sys.exit(main(["file_1.md"])) main("file_1.md") 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()
The video about CalculatorMemo 1.0
I made some changes in the code, eliminating the unuseful argparse use in the part of the markdown code, because I do not use the command line to make this. It also did not close the md file that was created, so I changed the code, making it better and simpler. In the video you will see the old code.