Gooey (Beta)安装使用说明

作者: 生信杂谈 | 来源:发表于2017-05-24 01:37 被阅读22次

    Gooey是一个将python命令行转换为界面行的程序包

    fig1

    快速入门

    安装指南

    Gooey可以非常容易的通过pip安装、

    pip install Gooey
    

    同样你也可以克隆项目到本地目录安装

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

    然后运行setup.py

    python setup.py install
    

    Gooey安装完以后请确认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进行翻译设置
           show_config=True,          # skip config screens all together
           program_name='name',       # 默认脚本名称
           program_description,       # 默认的 ArgParse 描述
           default_size=(610, 530),   #  GUI初始大小
           required_cols=1,           #  "Required" 选项一行多少列
           optional_cols=2,           #  "Optional" 选项一行多少列
           dump_build_config=False,   # Dump the JSON Gooey uses to configure itself
           load_build_config=None,    # Loads a JSON Gooey-generated configuration
           monospace_display=False)   # Uses a mono-spaced font in the output screen
    )
    def main():
      parser = ArgumentParser(...)
      # rest of code
    

    具体可以查看How does it Work章节查看每项设置的描述
    Gooey将尽力选择明智的小部件默认显示在GUI中。 但是,如果需要更精细的调整,可以使用插入式替换GooeyParser代替ArgumentParser。 这允许您控制GUI中的哪些窗口小部件显示。 参见:GooeyParser

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

    示例

    在下载了Gooey以后,我们可以在Examples Repository中下载一些已经准备好了的example 脚本,其可以给你一些快速的Gooey's的多变样式,小部件和功能的向导
    直接下载

    Gooey是什么

    Gooey将您的控制台应用程序转换为最终用户友好的GUI应用程序。 它让您专注于以熟悉的方式构建强大的可配置程序,而无需担心如何将其呈现给普通用户并与之进行交互。

    Why?

    因为我们喜欢命令提示,世界其他地方就像80年代初的丑恶遗物一样。 除此之外,程序通常不仅仅需要做一件事情,而且意味着提供以前意图构建GUI的选项,或尝试解释如何向控制台应用程序提供参数。 Gooey(希望)解决了这些问题。 它使程序易于使用,很漂亮的看!

    Who is this for?

    如果您正在为自己,其他程序员建立实用程序,或者产生要捕获并导管到另一个控制台应用程序的结果(例如* nix哲学实用程序),Gooey可能不是您的工具。 然而,如果你正在建立“运行和完成”,那么办公室风格的脚本,从点A到点B的东西,或者简单地针对非程序员的东西,Gooey是完美的工具 工作。 它允许您构建一个复杂的应用程序,因为您的心脏需要所有,同时获得GUI端免费。

    其是如何工作的?

    Gooey通过一个简单的装饰器附加到你的代码,无论哪个方法都有你的argparse声明。

    @Gooey
    def my_run_func():
      parser = ArgumentParser(...)
      # rest of code
    

    在运行时,它将解析您的Python脚本以获取对ArgumentParser的所有引用。 (当前不支持旧的optparse。)然后提取这些引用,并根据它们提供的“action”分配一个组件类型,最后用于组合GUI。

    映射表:

    --Parser Action-- --Widget-- --Example--
    store TextCtrl fig1
    store_const CheckBox fig2
    store_true CheckBox fig3
    store_false CheckBox fig4
    append TextCtrl fig5
    count DropDown fig6
    Mutually Exclusive Group RadioGroup fig7
    choice DropDown fig8

    Gooey会根据发现的选项,尽力选择合理的默认值。 目前,ArgumentParser._actions映射到以下WX组件。

    --Parser Action-- --Widget-- --Example--
    store TextCtrl fig1
    store_const CheckBox fig2
    store_true CheckBox fig3
    store_false CheckBox fig4
    append TextCtrl fig5
    count DropDown fig6
    Mutually Exclusive Group RadioGroup fig7
    choice DropDown fig8

    GooeyParser

    如果上述默认值不被执行,您可以使用ArgumentParser替换GooeyParser来控制确切的widget类型。 这将为您提供附加的关键字参数小部件,您可以向其提供要显示的组件的名称。 最好的部分? 您不必更改任何argparse代码来使用它。 放进去,所有东西都会为你准备好。

    示例

    from argparse import ArgumentParser
    ....
    
    def main(): 
        parser = ArgumentParser(description="My Cool Gooey App!")
        parser.add_argument('filename', help="name of the file to process") 
    

    鉴于上述,Gooey将选择一个正常的文本字段作为小部件类型,如下所示:


    fig1

    但是,通过在Gooey中使用widget名称,您可以显示更加用户友好的FileChooser来实现

    from gooey import GooeyParser
    ....
    
    def main(): 
        parser = GooeyParser(description="My Cool Gooey App!")
        parser.add_argument('filename', help="name of the file to process", widget='FileChooser') 
    
    fig2

    自定义小部件

    Widget Example
    DirChooser/FileChooser fig3
    DateChooser fig4

    国际化

    Gooey是拥有国际化设置,很容易移植到您的主机语言。 语言由Gooey装饰器的参数控制。

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

    所有程序文本都从外部存储在json文件中。 所以添加新的语言支持就像在gooey / languages /目录中粘贴几个键/值对一样简单。

    设置

    参数 概要
    advanced Toggles whether to show the 'full' configuration screen, or a simplified version
    show_config Skips the configuration all together and runs the program immediately
    language Tells Gooey which language set to load from the gooey/languages directory.
    program_name The name displayed in the title bar of the GUI window. If not supplied, the title defaults to the script name pulled from sys.argv[0].
    program_description Sets the text displayed in the top panel of the Settings screen. Defaults to the description pulled from ArgumentParser.
    default_size Initial size of the window
    required_cols Controls how many columns are in the Required Arguments section
    optional_cols Controls how many columns are in the Optional Arguments section
    dump_build_config Saves a json copy of its build configuration on disk for reuse/editing
    load_build_config Loads a json copy of its build configuration from disk
    monospace_display Uses a mono-spaced font in the output screen
    image_dir Path to the directory in which Gooey should look for custom images/icons
    language_dir Path to the directory in which Gooey should look for custom languages files

    Gooey中的所有内容都可以通过将参数传递给装饰器来定制。

    参数 概要
    advanced Toggles whether to show the 'full' configuration screen, or a simplified version
    show_config Skips the configuration all together and runs the program immediately
    language Tells Gooey which language set to load from the gooey/languages directory.
    program_name The name displayed in the title bar of the GUI window. If not supplied, the title defaults to the script name pulled from sys.argv[0].
    program_description Sets the text displayed in the top panel of the Settings screen. Defaults to the description pulled from ArgumentParser.
    default_size Initial size of the window
    required_cols Controls how many columns are in the Required Arguments section
    optional_cols Controls how many columns are in the Optional Arguments section
    dump_build_config Saves a json copy of its build configuration on disk for reuse/editing
    load_build_config Loads a json copy of its build configuration from disk
    monospace_display Uses a mono-spaced font in the output screen
    image_dir Path to the directory in which Gooey should look for custom images/icons
    language_dir Path to the directory in which Gooey should look for custom languages files

    运行模式

    Gooey拥有少量演示模式,因此您可以根据您的内容类型和用户的级别或体验来调整其布局。

    高级模式

    默认视图是“完整”或“高级”配置屏幕。 它有两种不同的布局,具体取决于它包装的命令行界面的类型。 对于大多数应用程序,平面布局将是随之而来的,因为它的布局最符合主命令的熟悉CLI模式,后跟许多选项(例如Curl,FFMPEG)。

    另一方面是列布局。 这一个最适合具有多个路径或由多个小工具组成的CLI,每个具有自己的参数和选项(认为:git)。 它显示左侧列的主要路径,右侧显示相应的参数。 这是一个很好的方法,可以将多种多样的功能整合到一个应用程序中。


    fig5

    这两个视图将参数分析器中的每个动作作为独特的GUI组件。 这使它非常适合向不熟悉命令行选项和/或控制台程序的用户呈现程序。 帮助消息显示在每个组件的旁边,使其尽可能清楚每个小部件的作用。

    设置样式风格:

    目前,布局不能通过参数(在TODO!上)进行明确的指定。 这些布局是根据代码库中是否使用子代码而构建的。 所以,如果你想触发列布局,你需要添加一个subparser到你的argparse代码。

    它可以通过Gooey装饰器中的高级参数切换。

    @gooey(advanced=True)
    def main():
        # rest of code   
    

    基础

    基本视图最适合用户熟悉Console应用程序的时间,但您仍然希望提供比简单终端稍微更精细的东西。 通过将gooey装饰器中的高级参数设置为False来访问基本显示。

    @gooey(advanced=False)
    def main():
        # rest of code  
    
    fig6

    无设置版本

    没有配置几乎可以预期:它不显示配置屏幕。 它跳到显示部分,开始执行主机程序。 这是为了改善小一点脚本的外观。


    fig

    自定义图标

    Gooey带有一组六个默认图标。 通过告诉Gooey在初始化时搜索其他目录,可以用自己的自定义图像/图标覆盖这些。 这可以通过Goeey装饰器的image_dir参数完成。

    @Gooey(program_name='Custom icon demo', image_dir='/path/to/my/image/directory')
    def main():
        # rest of program
    

    Gooey基于他们的文件名发现图像。 因此,例如,为了提供自定义配置图标,只需将图像文件名config_icon.png放在图像目录中即可。 这些是可以被覆盖的文件名:

    *program_icon.ico

    *success_icon.png

    *running_icon.png

    *loading_icon.gif

    *config_icon.png

    error_icon.png
    更多原创精彩内容敬请关注
    生信杂谈*:

    相关文章

      网友评论

        本文标题:Gooey (Beta)安装使用说明

        本文链接:https://www.haomeiwen.com/subject/bqrnxxtx.html