Gooey Magic

Gooey is an extension of Python that turns a command-line program into a GUI program with a single command. It can parse the command line arguments of the argparse module and turn it into a wxpython GUI control. Of course, Gooey itself also has a command-line parsing function – GooeyParser. Here is a brief introduction to the usage of Gooey, as well as its advantages and disadvantages.

Installing Gooey

The easiest way to install:

1
pip install Gooey

or:

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

Then run setup.py :

1
python setup.py install

Related dependencies:
wxpython
Installation: pip install wxpython (Windows needs to go to the official website to download the installation package)

Using Gooey to convert argparse

The simplest example is to convert the argparse parameter to a GUI control:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! -*- coding:utf-8 -*-
from gooey import Gooey
import argparse
@Gooey()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("test",help="this is a test" )
args=parser.parse_args()
print args
if __name__=="__main__":
main()

Run the screenshot:

Description: You can see Gooey as a decorator, and Gooey() can set parameters.

parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Gooey(advanced=Boolean, # toggle whether to show advanced config or not
language=language_string, # Translations configurable via json
show_config=True, # skip config screens all together
program_name='name', # Defaults to script name
program_description, # Defaults to ArgParse Description
default_size=(610, 530), # starting size of the GUI
required_cols=1, # number of columns in the "Required" section
optional_cols=2, # number of columns in the "Optional" section
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
image_dir # Path to the directory in which Gooey should look for custom images
language dir # Path to the directory in which Gooey should look for custom languages
)

The most commonly used parameters are program_name (title, default is the file name), default_size (interface size), image_dir (ico icon address, can be relative address, pay attention to backslash under windows)

1
2
3
4
5
6
7
8
9
@Gooey(program_name=u'This is a test script', default_size=(500,500))
def main():
Parser = argparse.ArgumentParser(description=u"test description content")
parser.add_argument("test",help="this is a test" )
args=parser.parse_args()
print args
if __name__=="__main__":
main()

Run the screenshot:

Using GooeyParse

Simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from gooey import Gooey, GooeyParser
@Gooey(program_name="test",image_dir=".\image") ##Note slash
def main():
parser.add_argument("test",help="this is a test")
##Select box
parser.add_argument(
"test2",
Metavar='Should I exlode?', ##Describe content
Help="this is test2", ##Help content
Choices=["Yes","No"], ##Select box
Default="Yes" ##default value
)
##checkbox
parser.add_argument(
'-f','--foo',
metavar="some flag",
Action="store_true", ##parameter type
help="")
##Text Select button
Parser.add_argument('datetime', metavar=u"time selection", help="date to process",widget='DateChooser',default="2017-02-15") #time selection button
args=parser.parse_args()
print args
if __name__=="__main__":
main()

Run the screenshot:

Description: Image_dir is set to the image directory in the current directory, the program will go to the image directory to find the corresponding image to override the default image, so the image name to be overwritten must be the default image name; you can see the running interface is divided into Required Arguments With the Optional Arguments parameter, the code corresponds to the former ‘test’, the ‘-test’ corresponds to the latter; the metavar represents the description information; the action indicates the control type; the help is the help information; the widget is the widget; the default is the default content.

Image file name under the image directory, used to cover the image on the interface:

  • program_icon.ico ico icon
  • success_icon.png Run successful icon
  • running_icon.png icon while running
  • loading_icon.gif icon when loading
  • config_icon.png Configuring images
  • error_icon.png Picture when an error occurs

The action content represents the parameter type, which corresponds to the corresponding control of wxpython:

  • choice DropDown

In addition to action, Gooey also provides some gadgets (Widgets)

  • DirChooser directory selection button tool
  • FileChooser file selection button tool
  • DateChooser time selection button tool

Advantages and disadvantages

Speaking of a summary of personal use, the advantages are convenient, no need for too much code, and the interface design is eliminated. The disadvantage is that it is not very suitable for operating very complex programs, and there are currently not many supported controls. In addition, there are a lot of bugs in the packager that need to be resolved.

Reference documentation:
GitHub address: [https://github.com/chriskiehl/Gooey] (https://github.com/chriskiehl/Gooey)
Official example: https://github.com/chriskiehl/GooeyExamples/tree/master/examples
Official documentation: https://github.com/chriskiehl/Gooey#how-does-it-work

本文标题:Gooey Magic

文章作者:nmask

发布时间:2017年02月15日 - 18:02

最后更新:2019年08月16日 - 15:08

原始链接:https://thief.one/2017/02/15/Gooey magic/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: