Gooey GUI for Python Scripts

GUI stands for “Graphical User Interface”, aka the part of a program that the user interacts with and sees.

Why add a GUI? I want to remove myself from being needed to run a report script that I wrote at work. Adding a GUI allows anyone to run my script without having to code or use the command line.

There are several GUI libraries in Python. A few I have heard of  are Tkinter (comes in the standard library), wxPython, PyQT and simplegui. I explored Tkinter back when I first got into Python. It was more intricate and offered more control over the look of your app, and took longer to pick up. Gooey is more of a pre-packaged GUI library.

I was unable to find much documentation on Gooey for Python. The Gooey Github page was most useful to me and helped me to do what I needed. The script posted in this blog helped as well.

Argparse comes stock with Python. You can install Gooey via the pip installer:

py pip install Gooey

My needs are simple: enable the user to supply three files and enter a number. Gooey was a good match. It has two branches in the library: some basic widgets piggyback off the argparse library and another part of the library uses a method called the GooeyParser. The GooeyParser offers more advanced widgets, like a file chooser. This was exactly what I was looking to use to pull in files for my script.

Below is a basic argparse/Gooey combination script. The  argparse version offers a handful of widgets such as checkboxes and dropdown, but I had trouble getting them to work with the GooeyParser (used in 2nd script).

Side note: Check out Python’s vars() built-in function. It returns the user’s arguments in a dictionary called user_inputs. Then we can get the values via the dictionary’s keys. Pretty nifty 🙂

from argparse import ArgumentParser
from gooey import Gooey

@Gooey(program_name="Email Campaign Reporting Generator", default_size=(575, 600))
def get_args():
    parser = ArgumentParser(description="A simple argument parser", epilog="This is where you might put example usage")
    parser.add_argument('Name', action="store", required=True, help='Help text for option X')
    parser.add_argument('Email', help='Help text for option Y', default=False)
    parser.add_argument('Campaign Number', help='Help text for option Z', type=int)
    parser.add_argument('Campaign Segment', choices=['A', 'B','All'], default='a', nargs='?')
    user_inputs = vars(parser.parse_args())
    print(user_inputs) 
    name = user_inputs['Name']
    campaign_number = user_inputs['Campaign Number']
    return parser.parse_args()

if __name__ == '__main__':
    get_args()

The @Gooey() part of the code is an advanced function known as a decorator in Python. Put simply, decorators are functions that modify the function to which they are attached.

Below is my script that uses the more advanced GooeyParser for its “FileChooser” widget. Gooey allows you to group widgets together and set how many widgets per line with the gooey_options{} parameter.

from gooey import Gooey, GooeyParser
 
@Gooey(program_name="Email Campaign Reporting Generator", default_size=(575, 600))
def get_args():
    parser = GooeyParser(description="Export campaign report spreadsheets and upload below.")
    top_group = parser.add_argument_group(gooey_options={'show_border': False,'columns': 1})
    top_group.add_argument('Contact List', help="Upload Send List (.xlsx)", widget='FileChooser') 
    top_group.add_argument('Opens List', help="Upload Opens List (.xlsx)", widget='FileChooser')
    top_group.add_argument('Unsubscribe List', help="Upload Unsubscribe List (.xlsx)", widget='FileChooser')
    bottom_group = parser.add_argument_group(gooey_options={'show_border': False,'columns': 1, 'required':False})
    bottom_group.add_argument('Campaign ID', action="store", help="Number found in the Campaign 'Reports' tab")
    bottom_group.add_argument('Campaign Segment', action="store", help="Enter 'A', 'B', or 'All'. All lists supplied must match segment.")
    user_inputs = vars(parser.parse_args())
    name = user_inputs['Name']
    return parser.parse_args()

if __name__ == '__main__':
    get_args()

Overall, Gooey knows what it wants to be: an easy to use GUI framework for Python. It does it well. Here’s a screenshot of my program’s shiny GUI:

gooey_gui_shot_2

Now that I have a GUI on top of my program and it delivers the expected output file, I’m hoping to take it one step further by packaging it up as a Windows .exe file. This would allow it to run as a desktop app on any Windows computer without the need to install Python or library dependencies. I’ve only begun exploring options to do this but the two libraries I’ve come across are pyinstaller, cx_Freeze and Py2Exe. Updates coming if I figure it out. Cheers 😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s