Python has a built-in GUI package, which allows easy building of forms like this:
The package used in this example is tkinter. The form above takes the value, entered in the entry field, squares it and displays it back in the entry field, thus the square button can be clicked again. Quite a standard application.
The interesting part in this article is to make the application Stand Alone, and send it to someone, who does not have python installed and does not need to see the console. These are the 3 small steps needed, once the code is written:
- Save the python code as *.pyw
- Install “pip install pyinstaller”
- Generate the exe file with “pyinstaller -w InputOutputConsole.pyw”
Once these steps are ready, this is the result in the same folder:
In the dist folder, there is an EXE file, which can run the small application. The code of the application is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from tkinter import * class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.grid() self.create_widgets() def create_widgets(self): self.display_label = Label(self, text="Enter value to square:") self.display_label.grid(row=0, column=0) self.input_field = Entry(self, bd = 5) self.input_field.grid(row=0,column=1) self.result_label = Label(self, text="Result will be here:") self.result_label.grid(row=1, column=0) self.calculation_button = Button(self, text="Square!", command=self.calculation) self.calculation_button.grid(row=3,column=3) self.quit_button = Button(self, text="Quit", command=self.master.destroy) self.quit_button.grid(row=4,column=10) def calculation(self): new_val = int(self.input_field.get())**2 self.result_label['text'] = new_val self.input_field.delete('0', 'end') self.input_field.insert('0', str(new_val)) if __name__ == "__main__": app = Application() app.mainloop() |
Enjoy it! Code is in GitHub as well.