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

简介

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

开发者通常很喜爱命令行,但对于普通用户来说是不可了解的。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@Gooeydef 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 argparsefrom gooey import Gooey, GooeyParserfrom message import display_messagerunning = 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()

国际化

Gooey 反对国际化,能够轻松地统一到目标语言中。语言通过 Gooey 装璜器进行管制:

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

控件

Gooey 提供了很多开箱即可的小控件,开发者能够间接引入应用,这里只简略列举几个。

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

.gif)

  • 日期/工夫选择器:

.gif)

  • 明码文本框:

  • 色彩选择器:

.gif)

  • 可搜寻下拉框:

.gif)

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