Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Automation Office Mini Software with Python Library PySimpleGUI

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the Python library PySimpleGUI how to make automation office small software, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Python plays an important role in operation and office automation. PySimpleGUI is a great automation auxiliary module that makes it easier for you to automate your daily tasks.

1 PySimpleGUI installation

Enter: pip install pysimplegui at the command line / terminal, wait for the installation to be completed, enter the python environment, enter import PySimpleGUI and enter correctly, and confirm the installation

2 two interface design modes of simple pop-up window made by PySimpleGUI

(1) single display interface (one-shot window)

Similar to a pop-up window, it appears once

Often used to prompt for information and collect information

(2) continuous display interface (Persistent window)

Continuous display unless manually turned off by the user

Often used as the main interface of software

2.2 making pop-up windows

The default library on the official website is abbreviated to sg. It is recommended to keep it uniform when using it, and sg is also used.

Pop-up window type: (the first is consistent with the second)

Sg.popup ('attention!')

Sg.popup_ok ('default pop-up')

Sg.popup_yes_no ('pop-up window with Yes and No buttons')

Sg.popup_cancel ('pop-up window with cancel button')

Sg.popup_ok_cancel ('pop-up window with OK and cancel buttons')

Sg.popup_error ('pop-up window with red error button')

Sg.popup_auto_close ('pop-up window that closes automatically in a few seconds')

After the last execution, the program automatically exits around 2 seconds. In addition to the above simple default function, you can also set parameters manually. The related parameters are as follows

For example, set up a customized small window to add relevant parameters.

Sg.popup ('this is a pop-up window', title='Hello', button_color= ('# A81B0C','# FFFFFF'), background_color='#F47264', pne_width=2, custom_text=' OK')

Output result: (the first parameter is the information to be displayed. It can be a single string or multiple strings. When there are multiple strings, the default line feeds. If it is a single string, you can specify the width of each line through pne_with.)

Automatic line wrapping when the first parameter is multiple strings (because the automatic form is very small in length and width, the title is not seen, but it does not mean that it is not displayed, as can be seen in the following example)

2.3 text content pop-up window

Using the popup_scrolled () method, add the content to be displayed in parentheses

Text =''Hello, everyone, let's learn about PySimpleGUI to make a simple graphical user interface.'' Sg.popup_scrolled (text,title='Hello')

The output is as follows: (the title here is displayed normally)

There are also relevant parameters in the text pop-up window, which can be set according to your own needs, as follows: (note that the parameters of the previous pop-up window can also be used in this kind of pop-up window, such as title just used)

2.4 get the pop-up window entered by the user

Using the popup_get_text () method, the parentheses are somewhat similar to the prompts in the input () statement, reminding the user to enter

Text1 = sg.popup_get_text ('Please enter text 1') print (text1) text2 = sg.popup_get_text (' Please enter text 2') print (text2)

The output result is: (when you click Ok, the output of the console will get the text entered by the user. If you click Cancel, the output will be None. Here, the instructions for user input and pop-up are executed twice. When you click Ok after the first input, the second window pops up automatically.)

This kind of pop-up window also has its own specific parameters to choose from, for example, since the input operation is carried out, sometimes you do not want others to see it when you enter the password, you can use the input display mode, as follows

The test carries on the password hidden input, can directly homeopathy the password entered by the user also in the form of pop-up window

2.5 File selection pop-up window

Directly use the sg.popup_get_file () method, and the contents in parentheses are also input prompts.

Once selected, the detailed address will be displayed directly in the input box, as follows

Then the same type of pop-up also has its own unique property parameter settings, as follows. Almost every parameter in it is a super commonly used parameter, which can be tested by yourself.

Default suffix, this parameter is also commonly used, such as in subpme click Save as, itself is a py file, when naming the file only need to output the file name, the suffix is automatically added.

2.6 folder selection window

Using the sg.popup_get_folder () method, the contents in parentheses are also input prompts. After executing the program, a window will pop up to select the file, and after mouse selection, the path of the folder will be added to the input.

2.7 Progress Bar pop-up window

Using the sg.one_pne_progress_meter () method, enter the relevant parameter settings in parentheses

For i in range (1000): sg.one_pne_progress_meter ('progress bar', I + 1, 1000, 'progress bar key',' this is a progress bar)

The output is: (will be loaded dynamically until it reaches 100%)

Of course, this kind of pop-up window also has its own unique parameter settings, as follows. For example, the common settings are vertical and horizontal, the upper and lower limits of the scroll bar, and so on.

For example, try the combined output of different parameters.

For i in range (1, 1000): sg.one_pne_progress_meter ('progress bar', I + 1, 1000, 'the progress bar key',', this is a progress bar, orientation='h', bar_color= ('# F47264progress,'# FFFFFF'))

The output is as follows:

(3) functional requirements for making simple compression software 3.1

After the software runs, a pop-up window lets the user select a folder.

After the user selects it, a pop-up window lets the user choose the location and name of the compressed package to save.

After the user input is completed, all the files in the folder will be compressed and packaged.

After the compression is completed, a window pops up to tell the user the size of the package.

3.2 functional disassembly

(1) after the software is running, a pop-up window lets the user select a folder

Popup_get_folder ()

(2) after user selection, a pop-up window lets the user select the location and name of the compressed package.

Popup_get_file ()

Save_as=True

Default_extension = 'zip'

(3) after user input, all files in the folder are compressed and packaged.

Zipfile module

(4) after the compression is completed, a window pops up to tell the user the size of the compressed package.

Os.stat () reads file information

Popup () pop-up window display data

3.3 all codes

Reference code: (mainly the details, for the compression path setting, need to deal with, otherwise the final decompression will appear many levels of unnecessary folders)

Import PySimpleGUI as sgimport zipfileimport osfolder = sg.popup_get_folder ('Please select a folder to be compressed') zip_path = sg.popup_get_file ('Please select the location of the compressed package to save', save_as=True, default_extension='zip', file_types= (('compressed package', '.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 / / 1024sg.popup (f 'compressed package size: {zip_size} KB')

The output is shown as follows: (perfect, ✿✿ flowers (°▽ °) ✿

Thank you for reading this article carefully. I hope the article "how to make Automation Office Mini Software in Python Library PySimpleGUI" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report