1 PySimpleGUI installation
On the CLI or terminal, enter PIP install PySimplegui. After the installation is complete, enter The Python environment, enter Import PySimplegui, and press Enter to check whether the installation is successful
2 PySimpleGUI makes a simple popover interface
PS: If you need Python learning materials, please click on the link below to obtain them
Free Python learning materials and group communication solutions click to join
2.1 Two interface design modes
(1) One-shot window
-
It’s kind of like a popover, once
-
It is used to prompt information and collect information
(2) Persistent window
-
Display continuously unless the user turns it off manually
-
Often used as the main interface of software
2.2 Making pop-ups
The default abbreviation of the library on the official website is SG. It is recommended to keep the same when using sg
Popover types :(the first and second are the same)
-
Sg. Popup (‘ attention! ‘)
-
Sg.popup_ok (‘ default popover ‘)
-
Sg.popup_yes_no (‘ popover with Yes and No buttons ‘)
-
Sg.popup_cancel (‘ popover with Cancel button ‘)
-
Sg.popup_ok_cancel (‘ popover with OK and Cancel buttons ‘)
-
Sg.popup_error (‘ popover with red error ‘)
-
Sg.popup_auto_close (‘ popover closes automatically after a few seconds ‘)
After the last execution, the program will automatically exit in about 2s. In addition to the simple default function above, you can also set parameters manually, related parameters are as follows
For example, set up a small customized window to add relevant parameters
Sg. popup(' this is popover ', title='Hello', button_color=('#A81B0C', '#FFFFFF'), background_color='#F47264', line_width=2, Custom_text =' ok ')Copy the code
Output result :(the first argument is the information to display, can be a single string, or multiple strings, the default newline, if a single string can specify the width of each line through line_with)
Wrapping when the first argument is multiple strings (the title is not seen because the automatic form is small, but it does not mean it is not displayed, as you can see in the following example)
2.3 Text pop-up window
Popup_scrolled () method is adopted, and the content to be displayed can be added in parentheses
Text = "" We are going to learn PySimpleGUI to make a simple graphical user interface. ''' sg.popup_scrolled(text,title='Hello')Copy the code
The output is as follows :(here the title is displayed normally)
The text content popover also has related setting parameters, which can be set according to your own needs, as follows :(note that the parameters of the previous popover can also be used in this kind of popover, such as the title used just now)
2.4 Obtaining a popup window for user input
Using the popup_get_text() method, the parenthesis content is similar to the prompt in the input() statement to remind the user to enter
Text2 = sg.popup_get_text(' please enter text1 ') print(text2)Copy the code
The output result is as follows :(when clicking Ok, the output end of the console will get the text input by the user. If clicking Cancel, the output end will be None. Here the user input pop-up command is executed twice, and clicking Ok after the first input will automatically pop up the second window)
This kind of popover also has its own specific parameters to choose from. For example, since the operation of input is carried out, sometimes the input password is not expected to be seen by others, so the input display mode can be adopted, as follows
Test password hidden input, can be directly the user input password in the form of popup display
2.5 Pop-up window for Selecting files
The sg.popup_get_file() method is used directly, and the contents in parentheses are also input prompts
The selection will display the detailed address directly in the input box, as shown below
So this kind of popup also has its own special attribute parameter Settings, as follows. Almost every parameter is a super common parameter, you can test yourself.
The default suffix, which is also commonly used, is the py file itself when you click Save as in sublime, and when you name the file you simply output the filename and the suffix is automatically added.
2.6 Folder Selection Window
Use the sg.popup_get_folder() method, and the contents in parentheses are also input prompts. After the execution of the program will pop up to select the file window, the mouse will select the folder path to add to the input box
2.7 Progress bar pop-up window
The sg.one_line_progress_meter() method is used, and parameter Settings are entered in parentheses
For I in range(1000): sg.one_line_progress_meter(' progress bar ', I + 1, 1000, 'this progress bar ',' this progress bar ')Copy the code
The output result is :(dynamic loading will be carried out until 100%)
Of course, this kind of popover also has its own unique parameter Settings, as follows. For example, the common Settings are vertical and horizontal, and the upper and lower limits of the scroll bar.
For example, try a combination of different parameters
for i in range(1, 1000): Sg.one_line_progress_meter (' progress ', I + 1, 1000, 'progress key',' Progress key', orientation='h', bar_color=('#F47264', '#FFFFFF') )Copy the code
The output is:
3 Create simple compression software
3.1 Functional Requirements
-
After the software runs, a pop-up window lets the user select a folder
-
A dialog box is displayed for you to select the location and name of the compressed package
-
After user input is complete, compress all files in the folder
-
After the compression is complete, a window pops up telling the user the size of the compressed package
3.2 Functional Disassembly
(1) After the software runs, a pop-up window lets the user select a folder
- popup_get_folder()
(2) After the user selects, a pop-up window is displayed for the user to select the location and name of the compressed package
-
popup_get_file()
-
save_as=True
-
default_extension = ‘zip’
(3) Compress and package all files in the folder after user input
- Zipfile module
(4) After the compression, a window pops up to tell the user the volume of the compressed package
-
Os.stat () reads file information
-
Popup () displays data
3.3 All Codes
Reference code :(mainly details, for the compression path Settings, need to deal with, or the final decompression will appear many layers of unnecessary folders)
Import PySimpleGUI as SG Import zipfile import OS Folder = sg.popup_get_folder(' Please select the folder to compress ') zip_path = Sg. popup_get_file(' please select the zip location to save ', save_as=True, default_extension='zip', file_types=((' zip', '.zip'), ) ) with zipfile.ZipFile(zip_path, 'w') as zipobj: for file in os.scandir(folder): zipobj.write(file.path, file.path.replace(folder, '.')) zip_size = os.stat(zip_path).st_size // 1024 sg.popup(f' zip_size: {zip_size} KB')Copy the code
Remember, sow flowers. (° °) Blue ✿)
PySimpleGUI graphic interface rendering and office automation small software production _Lys_828 blog -CSDN blog copyright belongs to the author all, such as infringement contact xiaobijian delete!