14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具

【导语】:将 Python 命令行转换为 GUI 的工具。

简介

Gooey 是一个将 Python 控制台程序转换为 GUI 应用程序的工具,让开发者专注于构建健壮的、可配置的程序,而无需担心应用程序如何呈现以及如何与用户交互。

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第1张图片

开发者通常很喜欢命令行,但对于普通用户来说是不可理解的。Gooey 很好地将两者结合起来,让开发者专注于代码,随心所欲的构建复杂的应用程序,并为用户提供友好的应用程序界面。

项目地址是:

https://github.com/chriskiehl/Gooey

快速开始

安装

安装 Gooey 的最简单方法是通过 pip:

pip install Gooey 

或者,可以通过先将项目 clone 到本地:

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

再运行 setup.py 文件:

python setup.py install

注意,Python 2 的用户必须手动安装 WxPython,从官网手动下载安装。

用法

Gooey 通过一个简单的装饰器附加到代码中,任何方法都有 argparse 声明(通常是main方法)。

from gooey import Gooey

@Gooey      <--- all it takes! :)
def main():
  parser = ArgumentParser(...)
  # rest of code

通过将参数传递给装饰器来配置不同的样式和功能:

# options
@Gooey(advanced=Boolean,          # 是否显示高级配置
       language=language_string,  # 配置语言,json字符串
       auto_start=True,           # 跳过配置
       target=executable_cmd,     # 显示设置子进程执行参数
       program_name='name',       # 程序名,默认是脚本文件名
       program_description,       # 描述,默认显示 ArgParse 的描述
       default_size=(610, 530),   # GUI 页面尺寸
       required_cols=1,           # 必填部分的列数
       optional_cols=2,           # 选填部分的列数
       dump_build_config=False,   # 保存自身的配置 JSON
       load_build_config=None,    # 加载指定的配置 JSON
       monospace_display=False)   # 在输出屏幕中使用单一间距的字体
)
def main():
  parser = ArgumentParser(...)
  # rest of code

可以使用 GooeyParser 来代替 ArgumentParser,GooeyParser 提供了更细节的一些配置和功能,包括指定已定义好的组件:

from gooey import Gooey, GooeyParser

@Gooey
def main():
  parser = GooeyParser(description="My Cool GUI Program!") 
  parser.add_argument('Filename', widget="FileChooser")
  parser.add_argument('Date', widget="DateChooser")
  ...

以下是官网提供的一个比较详细的例子:

"""
Example program to demonstrate Gooey's presentation of subparsers
"""

import argparse

from gooey import Gooey, GooeyParser
from message import display_message

running = True

@Gooey(optional_cols=2, program_name="Subparser Layout Demo")
def main():
    settings_msg = 'Subparser example demonstating bundled configurations ' \
                   'for Siege, Curl, and FFMPEG'
    parser = GooeyParser(description=settings_msg)
    parser.add_argument('--verbose', help='be verbose', dest='verbose',
                        action='store_true', default=False)
    subs = parser.add_subparsers(help='commands', dest='command')

    curl_parser = subs.add_parser(
        'curl', help='curl is a tool to transfer data from or to a server')
    curl_parser.add_argument('Path',
                             help='URL to the remote server',
                             type=str, widget='FileChooser')
    curl_parser.add_argument('--connect-timeout',
                             help='Maximum time in seconds that you allow curl\'s connection to take')
    curl_parser.add_argument('--user-agent',
                             help='Specify the User-Agent string ')
    curl_parser.add_argument('--cookie',
                             help='Pass the data to the HTTP server as a cookie')
    curl_parser.add_argument('--dump-header', type=argparse.FileType(),
                             help='Write the protocol headers to the specified file')
    curl_parser.add_argument('--progress-bar', action="store_true",
                             help='Make curl display progress as a simple progress bar')
    curl_parser.add_argument('--http2', action="store_true",
                             help='Tells curl to issue its requests using HTTP 2')
    curl_parser.add_argument('--ipv4', action="store_true",
                             help=' resolve names to IPv4 addresses only')

    # ########################################################
    siege_parser = subs.add_parser(
        'siege', help='Siege is an http/https regression testing and benchmarking utility')
    siege_parser.add_argument('--get',
                              help='Pull down headers from the server and display HTTP transaction',
                              type=str)
    siege_parser.add_argument('--concurrent',
                              help='Stress the web server with NUM number of simulated users',
                              type=int)
    siege_parser.add_argument('--time',
                              help='allows you to run the test for a selected period of time',
                              type=int)
    siege_parser.add_argument('--delay',
                              help='simulated user is delayed for a random number of seconds between one and NUM',
                              type=int)
    siege_parser.add_argument('--message',
                              help='mark the log file with a separator',
                              type=int)

    # ########################################################
    ffmpeg_parser = subs.add_parser(
        'ffmpeg', help='A complete, cross-platform solution to record, convert and stream audio and video')
    ffmpeg_parser.add_argument('Output',
                               help='Pull down headers from the server and display HTTP transaction',
                               widget='FileSaver', type=argparse.FileType())
    ffmpeg_parser.add_argument('--bitrate',
                               help='set the video bitrate in kbit/s (default = 200 kb/s)',
                               type=str)
    ffmpeg_parser.add_argument('--fps',
                               help='set frame rate (default = 25)',
                               type=str)
    ffmpeg_parser.add_argument('--size',
                               help='set frame size. The format is WxH (default 160x128)',
                               type=str)
    ffmpeg_parser.add_argument('--aspect',
                               help='set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)',
                               type=str)
    ffmpeg_parser.add_argument('--tolerance',
                               help='set video bitrate tolerance (in kbit/s)',
                               type=str)
    ffmpeg_parser.add_argument('--maxrate',
                               help='set min video bitrate tolerance (in kbit/s)',
                               type=str)
    ffmpeg_parser.add_argument('--bufsize',
                               help='set ratecontrol buffere size (in kbit)',
                               type=str)

    parser.parse_args()

    display_message()

if __name__ == '__main__':
    main()

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第2张图片

国际化

Gooey 支持国际化,可以轻松地一致到目标语言中。语言通过 Gooey 装饰器进行控制:

@Gooey(language='russian')
def main(): 
    ... 

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第3张图片

控件

Gooey 提供了很多开箱即可的小控件,开发者可以直接引入使用,这里只简单列举几个。

  • 文件选择器,DirChooser、FileChooser、MultiFileChooser、FileSaver、MultiFileSaver:

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第4张图片

  • 日期/时间选择器:

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第5张图片

  • 密码文本框:

  • 颜色选择器:

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第6张图片

  • 可搜索下拉框:

14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具_第7张图片

开源前哨 日常分享热门、有趣和实用的开源项目。参与维护 10万+ Star 的开源技术资源库,包括:Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET 等。

你可能感兴趣的:(14.3K Star!听说你不喜欢命令行?那快来试试这个转换成 GUI 的工具)