One line of code converts a Python program into a graphical interface application

Click " Python crawler and data mining " above to follow

Reply to " Books " to receive a total of 10 e-books of Python from beginner to advanced

now

day

Chickens

soup

On the seven strings of Ling Ling, listen to Song Fenghan.


The Gooey project supports converting (almost) any Python 2 or 3 console program into a GUI application with one line of code.

1. Get started quickly

Before you start, you need to make sure that Python and pip have been successfully installed on your computer. If not, you can visit this article: Super Detailed Python Installation Guide  to install.

If you use Python for data analysis, you can install Anaconda directly: Anaconda, a good helper for Python data analysis and mining , has built-in Python and pip.

In addition, I recommend that you use the VSCode editor, which has many advantages: The best partner for Python programming—VSCode detailed guide .

Please choose any of the following methods to enter the command to install the dependencies :
1. Open Cmd (Start-Run-CMD) in Windows environment.
2. Open Terminal in MacOS environment (command+space enter Terminal).
3. If you are using VSCode editor or Pycharm, you can directly use Terminal at the bottom of the interface.

(Method 1) The easiest way to install Gooey is through PIP:

pip install Gooey

(Method 2) Or, you can install Gooey by cloning the project to a local directory

git clone https://github.com/chriskiehl/Gooey.git

If your network does not support cloning from GitHub, please reply on the Python Practical Guide : Gooey download the project source code.

After decompression, enter the folder and run setup.py:

python setup.py install

2. How to use

Gooey attaches a simple decorator to the main function, and then uses GooeyParser to visualize all the parameters you need to use as text boxes, select boxes or even file select boxes.

For example, in the article downloaded from scihub literature , we need to enter two parameters: 1. Keyword, 2. Number of articles to be downloaded, using Gooey can change this way:

from gooey import Gooey, GooeyParser

@Gooey
def main():
    parser = GooeyParser(description="中文环境可用的scihub下载器 - @Python实用宝典") 
    parser.add_argument('path', help="下载路径", widget="DirChooser")
    parser.add_argument('keywords', help="关键词")
    parser.add_argument('limit', help="下载篇数")
    args = parser.parse_args()
    search(args.keywords, int(args.limit), args.path)

GooeyParser is the same as ArgumentParser. You can use add_argument to add input parameters. The difference is that GooeyParser provides visual options:

parser.add_argument('path', help="下载路径", widget="DirChooser")

In this line of code, the widget parameter provides a directory selector ( widget="DirChooser" ) to the args.path variable . The help parameter is used to remind the user of the role of the selector. The effect is as follows:



When you do not provide widget parameters, the program uses the text input box by default.

parser.add_argument('keywords', help="关键词")
parser.add_argument('limit', help="下载篇数")



Gooey will automatically arrange your parameters, so you don't need to worry about the display of each text box or selection box. In the code:

args = parser.parse_args()
search(args.keywords, int(args.limit), args.path)

args = parser.parse_args() can convert all text entered by the user into the variable value of the corresponding object, and the corresponding variable value can be extracted directly through args.var.

The complete code and effect of this simple visualization program are as follows:

Swipe up to see more codes

import asyncio
from scihub import SciHub
from gooey import Gooey, GooeyParser

def search(keywords: str, limit: int, path: str):
    """
    搜索相关论文并下载

    Args:
        keywords (str): 关键词
        limit (int): 篇数
        path (str): 下载路径
    """
    sh = SciHub()
    result = sh.search(keywords, limit=limit)
    print(result)

    loop = asyncio.get_event_loop()
    # 获取所有需要下载的scihub直链
    tasks = [sh.async_get_direct_url(paper["url"]) for paper in result.get("papers", [])]
    all_direct_urls = loop.run_until_complete(asyncio.gather(*tasks))
    print(all_direct_urls)

    # 下载所有论文
    loop.run_until_complete(sh.async_download(loop, all_direct_urls, path=path))
    loop.close()

@Gooey
def main():
    parser = GooeyParser(description="中文环境可用的scihub下载器 - @Python实用宝典") 
    parser.add_argument('path', help="下载路径", widget="DirChooser")
    parser.add_argument('keywords', help="关键词")
    parser.add_argument('limit', help="下载篇数")
    args = parser.parse_args()
    search(args.keywords, int(args.limit), args.path)

main()


If this code wants to run perfectly, please combine  the python super literature batch search download tool  scihub.py that you have to know .

You can also use your own program for graphical interface, it doesn't matter.

The effect is as follows:

3. Supported widget components

All supported widget components are as follows:

1. Check the box  widget="CheckBox" 

2. Drop-down box  widget="DropDown"

3. Mutually exclusive selection box  widget="RadioGroup"

4. Selection boxes for various target types

File selection box  widget="FileChooser"
directory selection box  widget="DirChooser"
multi-file selection box  widget="MultiFileChooser"
file save directory  widget="FileSaver"

5. Date/time picker  widget="DateChooser/TimeChooser"

6. The password input box  wiget="PasswordField"

7. Multi-select list box  widget="Listbox"

8. Color picker  widget="ColourChooser"

9. Filterable drop-down box  widget="FilterableDropdown"

10.滑片 widget="Slider"

4. Pack

After everything is tested and used normally, you can package this visualization program into an exe executable file through pyinstaller.

1. Write PyInstaller buildspec

PyInstaller uses buildspec to determine how to bundle the project. You can download build.spec.txt by replying to buildspec in the backend of Python Practical Collection  .

You only need to change two lines of code after downloading:

As follows:

With r in front of the path, there is no need to enter two diagonal bars'\'.

2. Execute packaging commands

In order to be able to use PyInstaller, we need to install this module using pip:

pip install pyinstaller

Then enter the folder where build.spec.text is located, and execute the following command to package the program:

pyinstaller build.spec.txt



After the packaging is complete, a dist folder will be generated in the current folder, which contains the executable file you generated, and the packaging is successful.

------------------- End -------------------

Recommendations of previous wonderful articles:

Welcome everyone to like , leave a message, forward, reprint, thank you for your company and support

If you want to join the Python learning group, please reply in the background [ Enter the group ]

Thousands of rivers and mountains are always in love, can you click [ Looking ]

/Today's message topic/

Just say a word or two~~

Guess you like

Origin blog.csdn.net/pdcfighting/article/details/113930717