1. Overview
This article focuses on the use of Python to write a small program that can calculate the number of skipped parking spaces with a specific number. It also utilizes a third-party library, wxPython.GUI interface, packaged into an executable file.
In reality, there is such a practical case, the number of underground garage of a building has more than 2,000 parking spaces, the parking spaces are divided into five sub-districts, each sub-district has 400-500 parking spaces ranging in number. And the number of parking spaces has been programmed on the map one by one, for example, the starting number is 001, the termination number is 720, but the marketing personnel in the preparation of the number of parking spaces to avoid the number of parking spaces with the number 4 and the number of numbers 18, for example, 4, 14, 18, 24, 40, 118, 114, etc., so that we can not be directly used to terminate the number minus the number of the starting number and then add the number of one algorithm to calculate out the number of parking spaces.
So to make it easier to count the total number of parking spaces for this kind of number hopping, I wrote this simple code. This code utilizes very basic python knowledge, such as string manipulation, list manipulation, for loops, input functions, custom functions and so on, from shallow to deep gradually improve the whole code. It's very suitable for Python beginners who have just finished learning list and string related operations to practice using. wxPython part of the application is also very basic usage, mainly user input text box, static text, and the basics of button control.
In order to facilitate the use of this widget for people who don't know how to use Python and don't have a Python development environment on their computers, we need to make a GUI interface and package it as aexecutable file。
2.Code realization
(1) Python environment code implementation
This section is detailed in my other article:A small program written in Python to calculate the number of parking spaces skipped with a specific number and packaged as an executable using wxPython to make a GUI interface (Part1)
(2) wxPython create GUI interface
① Introduction to wxPython
wxPython is an excellent set of GUI graphics library for the Python language. It allows Python programmers to easily create complete, full-featured GUIs.user. (# originating fromBaidu online encyclopedia)
② Install wxPython
wxPython is a third-party library for Python that needs to be installed with pip, as in the following code:
pip install wxpython
③ Create a simple window
The following code is the basic code to create a window, see the comments in the code for details:
-
import wx # Import wxpython modules
-
-
-
class MyFrame(): # Define a subclass to give us more control over the content and appearance of the window.
-
def __init__(self): # Define initialization methods
-
# Set the title and size of the frame
-
.__init__(self, None, -1, title='Calculate the number of parking spaces applet', size=(400, 300))
-
-
-
# Run as main program
-
if __name__ == '__main__':
-
app = () # Create an application instance
-
frame = MyFrame() # Example of creating a window
-
() # Make the window visible
-
() # Call the MainLoop() method of the application instance to enter the main event loop
The above code runs and creates a window as shown below:
④ Add controls to the window
Next we add controls on this window, add controls need to add the canvas first, all the controls are laid out to the canvas, add the canvas code is as follows, where -1 for the ID value, you can customize, you can also use -1 by wxPython to automatically generate a new ID.
panel = (self, -1)
(panel, -1, 'Please enter the starting parking space number', pos=(30, 30))
self.start_no_text = (panel, -1, '001', pos=(150, 30), size=(100, -1))
The code runs as shown below:
-
# Add static text, set parent container to panel, ID value to -1, display text content and position
-
(panel, -1, 'Please enter the starting parking space number', pos=(30, 30))
-
(panel, -1, 'Please enter the termination parking space number', pos=(30, 60))
-
(panel, -1, 'Enter the digits not included in the number, separated by commas', pos=(30, 90))
-
(panel, -1, 'Enter the digits to be skipped in the number, separated by commas', pos=(30, 150))
-
(panel, -1, 'The total number of parking spaces is', pos=(30, 240))
-
-
# Add input text box, set parent container as panel, ID value as -1, default text content, position and size
-
self.start_no_text = (panel, -1, '001', pos=(150, 25), size=(100, -1))
-
self.end_no_text = (panel, -1, pos=(150, 55), size=(100, -1))
-
self.not_in_no_text = (panel, -1, pos=(30, 115), size=(220, -1))
-
self.pass_no_text = (panel, -1, pos=(30, 175), size=(220, -1))
-
self.total_no_text = (panel, -1, pos=(150, 235), size=(100, -1))
The effect after running is as follows:
I'll add the button control below.The constructor creates the button, which has the following constructor:
= (panel, -1, 'Click me for calculations', pos=(30, 205), size=(220, -1))
The effect of running after adding is as follows:
From the effect the button looks too close to the text box, to emphasize the button we create a font thatFonts are classesto create a font instance, use the following constructor:
The following is the code to create the font:
font = (10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
Setting the font can be done using the SetFont() method as in the following code:
(font)
The running effect is as follows:
After that we add a multiline text control to display the number of all parking spaces, the multiline text control in theCreate is to specify the style as wx.TE_MULTILINE can be, the following code:
-
# Create a text box, specify style wx.TE_MULTILINE for multi-line text and wx.TE_READONLY for read-only.
-
self.every_no_text = (panel, -1, pos=(30, 295), size=(220, 220),
-
style=wx.TE_MULTILINE | wx.TE_READONLY)
The results of the run are as follows:
The above is the complete GUI interface, the overall framework we are considered to have completed the creation of the next is that we do the corresponding user events related to the code, otherwise we have no way to use this window file. The following is the complete code of the GUI interface in front of us, which can be used for research and study:
-
import wx # Import wxpython modules
-
-
-
class MyFrame(): # Define a subclass to give us more control over the content and appearance of the window.
-
def __init__(self): # Define initialization methods
-
# Set the title and size of the frame
-
.__init__(self, None, -1, title='Calculate the number of parking spaces applet', size=(300, 600),
-
style= | wx.CLOSE_BOX)
-
# Add the canvas, the canvas is the container for all controls, we add text boxes, buttons and other controls are laid out on the canvas
-
panel = (self, -1)
-
-
# Add static text, set parent container to panel, ID value to -1, display text content and position
-
(panel, -1, 'Please enter the starting parking space number', pos=(30, 30))
-
(panel, -1, 'Please enter the termination parking space number', pos=(30, 60))
-
(panel, -1, 'Enter the digits not included in the number, separated by commas', pos=(30, 90))
-
(panel, -1, 'Enter the digits to be skipped in the number, separated by commas', pos=(30, 150))
-
(panel, -1, 'The total number of parking spaces is', pos=(30, 240))
-
(panel, -1, 'The specific car park numbers for each car park are as follows', pos=(30, 270))
-
-
# Add input text box, set parent container to panel, ID value to -1, default text content, position and size
-
self.start_no_text = (panel, -1, '001', pos=(150, 25), size=(100, -1))
-
self.end_no_text = (panel, -1, pos=(150, 55), size=(100, -1))
-
self.not_in_no_text = (panel, -1, pos=(30, 115), size=(220, -1))
-
self.pass_no_text = (panel, -1, pos=(30, 175), size=(220, -1))
-
self.total_no_text = (panel, -1, pos=(150, 235), size=(100, -1))
-
# Create a text box, specify style wx.TE_MULTILINE for multi-line text and wx.TE_READONLY for read-only.
-
self.every_no_text = (panel, -1, pos=(30, 295), size=(220, 220),
-
style=wx.TE_MULTILINE | wx.TE_READONLY)
-
-
# Create fonts
-
font = (10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
-
-
# Add a button control to the panel
-
= (panel, -1, 'Click me for calculations', pos=(30, 205), size=(220, -1))
-
# Set the font
-
(font)
⑤ Write logic code
Due to the fact that in a chapterA small program written in Python to calculate the number of parking spaces skipped with a specific number and packaged as an executable using wxPython to make a GUI interface (Part1)Already in the python environment can perfectly realize the function we want, so most of the code can be used directly. In wxPython controls you can use the GetValue() method if you want to get the text content of the control, and the SetValue() method if you want to set the text content. This corresponds to the input function and the print() function from the previous chapter.
First we have to bind the click event method to the button first, as in the following code:
(wx.EVT_BUTTON, , )
We also need to define the click event method, this method within our main code, most of the code is directly pasted from the previous chapter, just in individual places to adapt the following code:
-
def OnClick(self, event):
-
-
# Get the data entered by the user
-
start_no = self.start_no_text.GetValue()
-
end_no = self.end_no_text.GetValue()
-
pass_no = self.pass_no_text.GetValue()
-
not_in_no = self.not_in_no_text.GetValue()
-
# Create an empty list to store the parking space numbers
-
parking_no_lst = []
-
# Create an empty list for storing not_in_no
-
not_in_no_lst = []
-
# Separate comma-separated user-entered data into separate lists
-
if not_in_no != "": # Add this if statement to determine that the following code will be executed only if there is data in not_in_no
-
for item in not_in_no.replace(",", ",").split(","):
-
not_in_no_lst.append(item)
-
-
# Create an empty list to store the pass_no
-
pass_no_lst = []
-
# Separate comma-separated user-entered data into separate lists
-
if pass_no != "": # Add this if statement to determine that the following code will be executed only if there is data in pass_no
-
for item in pass_no.replace(",", ",").split(','):
-
pass_no_lst.append(item)
-
# Get the length of the parking space entered by the user
-
l = len(start_no)
-
-
# Iterate through all the digits from the start to the end with a for sequence.
-
# As the input function to obtain the data format for the string format, so you need to use the int () function to convert to integer type
-
for i in range(int(start_no), int(end_no) + 1):
-
# Judgment
-
if self.is_lst_in_str(not_in_no_lst, str(i)) or self.is_no_in_lst(str(i), pass_no_lst):
-
continue
-
else:
-
parking_no_lst.append("0" * (l - len(str(i))) + str(i))
-
# Calculate the length of the list with the len function to get the total number of parking spaces, and use the SetValue method to set the display text of the total_no_text control.
-
self.total_no_text.SetValue(str(len(parking_no_lst)))
-
# Output all parking space numbers
-
self.every_no_text.Clear() # Empty the contents first
-
for i in range(len(parking_no_lst) - 1):
-
self.every_no_text.AppendText(parking_no_lst[i] + ',')
-
self.every_no_text.AppendText(parking_no_lst[-1])
-
-
def is_no_in_lst(self, no, lst):
-
if no in lst:
-
return True
-
else:
-
return False
-
-
# Define a function that determines whether an element in a list is in a string or not
-
def is_lst_in_str(self, lst, input_str):
-
for item in lst:
-
if item in input_str:
-
return True
-
return False
The code runs as follows, if you do not enter the contents of the jump sign will not report an error, but also can run correctly:
Finally there is the validation of the input to match the format we require, which requires the use of the validator in wxPython (Validator), the validator (Validator) belongs to the higher-order usage, this article will not expand the description, please research on their own. Here is the complete code.
-
import wx # Import wxpython modules
-
-
-
class MyFrame(): # Define a subclass to give us more control over the content and appearance of the window.
-
def __init__(self): # Define initialization methods
-
# Set the title and size of the frame
-
.__init__(self, None, -1, title='Calculate the number of parking spaces applet', size=(300, 600),
-
style= | wx.CLOSE_BOX)
-
# Add the canvas, the canvas is the container for all controls, we add text boxes, buttons and other controls are laid out on the canvas
-
panel = (self, -1)
-
-
# Create fonts
-
font = (10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
-
-
# Add static text, set parent container to panel, ID value to -1, display text content and position
-
(panel, -1, 'Please enter the starting parking space number', pos=(30, 30))
-
(panel, -1, 'Please enter the termination parking space number', pos=(30, 60))
-
(panel, -1, 'Enter the digits not included in the number, separated by commas', pos=(30, 90))
-
(panel, -1, 'Enter the digits to be skipped in the number, separated by commas', pos=(30, 150))
-
(panel, -1, 'The total number of parking spaces is', pos=(30, 240))
-
(panel, -1, 'The specific car park numbers for each car park are as follows', pos=(30, 270))
-
-
# Add input text box, set parent container as panel, ID value as -1, default text content, position and size
-
self.start_no_text = (panel, -1, '001', pos=(150, 25), size=(100, -1))
-
self.end_no_text = (panel, -1, pos=(150, 55), size=(100, -1))
-
self.not_in_no_text = (panel, -1, pos=(30, 115), size=(220, -1))
-
self.pass_no_text = (panel, -1, pos=(30, 175), size=(220, -1))
-
self.total_no_text = (panel, -1, pos=(150, 235), size=(100, -1))
-
self.total_no_text.SetFont(font)
-
# Create a text box, specify style wx.TE_MULTILINE for multi-line text and wx.TE_READONLY for read-only.
-
self.every_no_text = (panel, -1, pos=(30, 295), size=(240, 220),
-
style=wx.TE_MULTILINE | wx.TE_READONLY)
-
-
# Add a button control to the panel
-
= (panel, -1, 'Click me for calculations', pos=(30, 205), size=(220, -1))
-
# Set the font
-
(font)
-
# Bind events to buttons
-
(wx.EVT_BUTTON, , )
-
-
# Define event methods
-
def OnClick(self, event):
-
-
# Get the data entered by the user
-
start_no = self.start_no_text.GetValue()
-
end_no = self.end_no_text.GetValue()
-
pass_no = self.pass_no_text.GetValue()
-
not_in_no = self.not_in_no_text.GetValue()
-
# Create an empty list to store the parking space numbers
-
parking_no_lst = []
-
# Create an empty list for storing not_in_no
-
not_in_no_lst = []
-
# Separate comma-separated user-entered data into separate lists
-
if not_in_no != "": # Add this if statement to determine that the following code will be executed only if there is data in not_in_no
-
for item in not_in_no.replace(",", ",").split(","):
-
not_in_no_lst.append(item)
-
-
# Create an empty list to store the pass_no
-
pass_no_lst = []
-
# Separate comma-separated user-entered data into separate lists
-
if pass_no != "": # Add this if statement to determine that the following code will be executed only if there is data in pass_no
-
for item in pass_no.replace(",", ",").split(','):
-
pass_no_lst.append(item)
-
# Get the length of the parking space entered by the user
-
l = len(start_no)
-
-
# Iterate through all the digits from the start to the end with a for sequence.
-
# As the input function to obtain the data format for the string format, so you need to use the int () function to convert to integer type
-
for i in range(int(start_no), int(end_no) + 1):
-
# Judgment
-
if self.is_lst_in_str(not_in_no_lst, str(i)) or self.is_no_in_lst(str(i), pass_no_lst):
-
continue
-
else:
-
parking_no_lst.append("0" * (l - len(str(i))) + str(i))
-
# Calculate the length of the list with the len function to get the total number of parking spaces, and use the SetValue method to set the display text of the total_no_text control.
-
self.total_no_text.SetValue(str(len(parking_no_lst)))
-
# Output all parking space numbers
-
self.every_no_text.Clear() # Empty the contents first
-
for i in range(len(parking_no_lst) - 1):
-
self.every_no_text.AppendText(parking_no_lst[i] + ',')
-
self.every_no_text.AppendText(parking_no_lst[-1])
-
-
def is_no_in_lst(self, no, lst):
-
if no in lst:
-
return True
-
else:
-
return False
-
-
# Define a function that determines if an element in a list is in the string
-
def is_lst_in_str(self, lst, input_str):
-
for item in lst:
-
if item in input_str:
-
return True
-
return False
-
-
-
if __name__ == '__main__': # Run as main program
-
app = () # Create an application instance
-
frame = MyFrame() # Example of creating a window
-
() # Make the window visible
-
() # Call the MainLoop() method of the application instance to enter the main event loop
⑥pyinstaller to package and generate exe file
Finally, we can use pyinstaller to package the exe file in thepycharmof the control terminal can be generated with the following code:
Pyinstaller -F -w Calculate the number of parking spaces applet.py
Note that Pyinstaller is a third-party library and needs to be pip installed in advance.
Finally we will get an exe file as below, I have uploaded the file as well, if you need it please download it yourself.
3. Summary
This updated blog post is divided into two articles, but each is more than 10,000 words, this gadget implements a very simple function, but allows people to follow this article from scratch step by step to complete the entire code. I hope to bring some help to the readers.