Hi, I’m Mingo.
Early readers will remember that last year on This day, on Valentine’s Day, Mingo gave you a little script written in Python that converts a picture of your goddess into a picture of a character. It looks like this.
This is before the conversion
This is the converted
The entire script is only about 20 lines of code, but it is well known that Python scripts require a Python environment to run, and a bunch of additional Python libraries to install if the script uses third-party libraries. This means that, for a student who has no experience with Python, it will be difficult to get scripts running.
Just this year’s Valentine’s Day, I have not thought of a good idea, it is better to put this tool last year, using tkinter package into an exe executable file, so that all people can use up ~
Let’s take a look at the finished interface of this EXE program
I put the exe uploaded to the network location: wws.lanzous.com/igrjGlnxw9i
Next, I’ll recount how I wrote this program in two hours with absolutely no GUI programming background.
0. Preparation
Before you actually start writing code, you need to do some preliminary research. What do you need to do to write such a GUI interface?
- Prototyping: What do you want your software to look like? Find out if there’s a tool that can prototype this kind of software.
- Learn GUI programming: Learn about Python’s GUI programming frameworks and pick the one that is most accessible
- Package as EXE: Understand and learn how to package Python scripts as exe files
1. Draw the model
Anyone who has written a book will know that you don’t just start writing an essay after you’ve chosen the question. The general outline of a book should be listed before writing it.
Writing GUI software interfaces is the same, before the actual development of the interface, it is necessary to draw a model of the software according to the function.
In this prototype diagram, you can intuitively see what functions the software is to achieve, how to achieve? Do I pull it down or do I type it manually?
For the software prototype drawing, I used the online tool http://drao.io that I had been using before.
From the prototype diagram can be seen, I this software to achieve some basic functions
Started making
: Converts the source image into the target image composed of charactersTo make
: If the initial production is not satisfied, you can click here to make again, so you do not have to close and open the softwareZoom in the preview
: After conversion, it will be saved to the specified directory. Click this button to preview directlyDownload the source code
: Maybe some students want to learn this code, so I added this entry.
2. GUI programming
Before today, I had never learned GUI programming frameworks. After a brief understanding and comparison, I chose the lightest built-in GUI framework – Tkinter.
Yeah, it’s built in.
However, if you import tkinter from your own computer, it is possible to report that the tkinter library was not found.
What’s going on here?
It turns out that this option is already available when you install the Python interpreter.
So if you forgot to check this option before, I did a quick search to reinstall it, and it seems to be a bit of a hassle.
If you have the Python interpreter installed, double-click the Python interpreter installation file and it will bring up a screen like this. If you click Modify, it will bring up the TCL/TK screen again. Next will re-install Tkinter on your computer along the way.
After the installation of Tkinter, I can learn various controls according to the prototype diagram. After simple sorting, I need to learn
- Label: indicates the Label used to display text
- Button: Button used to trigger events
- Combobox: Drop-down list for selecting fonts
- Canvas: A Canvas used to display images
- Filedialog: Dialog box used to select the image and save path
- Messagebox: message pop-up window, used to give users prompts and warnings
With this learning path, after watching a few demos with Google, you can start coding directly.
3. Pack the file as an EXE
Package tools. Pyinstaller is known to be a good tool to use for single-file scripts.
Use the following command to generate a dist directory, which opens with a love-tool.exe file.
$ pyinstaller -F -w --hidden-import=tkinter -i ming.ico -y love_tool.py
Copy the code
Several of these parameters mean:
-F
: Make independent executable programs-w
: Do not open the console window when exe is running-i
: Specifies the icon of the software--hidden-import=tkinter
: Imports the Tkinter package
Since my Python program will reference two local images, the exe cannot be executed immediately after it is generated. I have to put my two images in the same directory of the EXE first.
At this point, double-click love-tool.exe again to work
4. Exception handling
When the user has not selected the source image, it is theoretically impossible to convert, so in this case click start, will give a hint
Similarly, if the user has not completed the conversion, also cannot be enlarged preview.
5. Write at the end
This software is not to explain the code, the main is to repeat the entire development process, have students interested in the code, you can point the link (wws.lanzous.com/igrjGlnxw9i… Exe, click to download the source code.
Finally, hopefully this tool will add a little fun between you and the goddess