- How to Build a GUI Program with Python
- By Aisha Mohammed
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: YueYongDev
- Proofreader: LJC, Liang2028
How to build GUI programs using Python
This tutorial uses a Python library called Tkinter to build an application that can predict the upcoming menstrual cycle based on the timing of the most recent period.
I was recently learning GUI programming and it occurred to me that it would be fun to apply what I learned to the above project. The project is to implement a GUI (graphical user interface) program that calculates a user’s due date (EDD) when they enter the date of their last menstruation (LMP). I built this program using the Tkinter module.
Tkinter, or “TK Interface”, is one of Python’s GUI libraries. GUI implements user interaction by displaying ICONS and widgets on a computer. TKinter provides a variety of components, such as Labels, buttons, text boxes, chechboxes, etc., with various functions.
In this article, I’ll take an incremental approach to building the application in stages, adding code to the application, and then testing it. The stages are:
-
Design the user interface on paper
-
Create a framework that covers multiple components
-
Continually add components at the appropriate size and location
-
Add callback functions to your program to respond to user actions when they interact
Drawing the User Interface
I imagined what the app would look like and made two simple sketches. I chose to use the second version of the sketch, which shows conception on the left and menstruation on the right.
The basic framework for creating pages
I implemented a simple framework for the program using the Frame component. I created two frames of the same size. The dimensions are specified here, since the entire program has no components at this point (so you usually need to specify them manually beforehand). I also gave each frame a different color to distinguish the Windows they cover. See the code here.
Add a component to the left frame
I use the Label component to display the photo and place it in the frame on the left. This component can be used to display text or images. The source image. At this point I removed the code that limits the size of the frame, because the widgets in the current frame can already determine their size.
To the right side of the frame
I added the title (EDD CALCULATOR) using the tag widget and added a hint message underneath it using the message widget. I used the Entry widget in the margins and added the “calculate EDD” button using the button component. At first, I tried to use gird or Place layout managers to orchestrate components because it seemed easier to use. Instead, I didn’t get the results I expected. Finally, I select the Pack Layout Manager and use some padding inside and outside the component to place the component where I want it to be. I then changed the color of the frame on the right to “harmonize” with the color of the frame on the left. Refer to the code here.
Add a callback function
My application needs to have some interaction logic. To do this, I added the ability to enter data into a text box, which computes the EDD based on the input data received, and uses the MessageBox component to return information to the user. If the date is entered in the specified format, the program returns a message containing the user’s EDD information. Otherwise, an error message is returned. The following is an example.
Here’s the full link, which you can check out on Github. Based on this, you can use it freely to create and modify your own applications. I look forward to seeing your results.
conclusion
It was an interesting project for me. I like it better than the one or two Tkinter projects I’ve found online in the past. Because I don’t really know what’s going on “behind the scenes” with projects I find on the Internet. The whole process makes me feel like I’m copying code, which is obviously bad.
Thanks for reading to the end! If you have any questions or comments, please comment below. I also posted this article on my personal website. You can see my other articles there.
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.