Explain Gooey python library.
Gooey is a Python library that helps in creating graphical user interfaces (GUIs) for command-line applications. It’s built on top of the Tkinter library and aims to make it easy for developers to create user-friendly interfaces for their command-line programs.
Key Features:
- Automatic GUI Generation: Gooey automatically generates a GUI for your command-line application based on the arguments and options you define.
- Cross-Platform: Gooey is designed to work on various operating systems, including Windows, macOS, and Linux.
- Integration with Existing Code: You can use Gooey with existing command-line scripts without modifying the underlying functionality. Gooey adds a GUI layer on top of the existing code.
- Customization: While Gooey provides automatic GUI generation, you can customize the appearance and behavior of the generated GUI to some extent.
Installation:
pip install Gooey
Basic Example:
Consider a simple command-line script that takes two input parameters and prints them:
# script.py
import argparse
def main():
parser = argparse.ArgumentParser(description=”A simple command-line script”)
parser.add_argument(‘input1’, help=’First input parameter’)
parser.add_argument(‘input2’, help=’Second input parameter’)
args = parser.parse_args()
print(f”Input 1: {args.input1}”)
print(f”Input 2: {args.input2}”)
if __name__ == “__main__”:
main()
Now, let’s use Gooey to turn this into a graphical interface:
# gooey_script.py
from gooey import Gooey, GooeyParser
@Gooey
def main():
parser = GooeyParser(description=”A simple Gooey script”)
parser.add_argument(‘input1’, help=’First input parameter’)
parser.add_argument(‘input2’, help=’Second input parameter’)
args = parser.parse_args()
print(f”Input 1: {args.input1}”)
print(f”Input 2: {args.input2}”)
if __name__ == “__main__”:
main()
Now, when you run python gooey_script.py
, Gooey will automatically generate a GUI for the script, allowing users to input values using text fields.
Using Gooey Options:
Gooey provides additional options to customize the generated GUI. For example, you can specify the program name, disable the success message, and more.
@Gooey(
program_name=”My Gooey Script”,
show_success_modal=False,
navigation=”TABBED”,
suppress_gooey_flag=True
)
def main():
# …
Adding Widgets:
Gooey allows you to specify different widgets for input parameters. For instance, you can use the FileChooser
widget for file inputs:
@Gooey
def main():
parser = GooeyParser(description=”Gooey script with widgets”)
parser.add_argument(‘input_file’, help=’Select a file:’, widget=’FileChooser’)
parser.add_argument(‘output_dir’, help=’Select a directory:’, widget=’DirChooser’)
args = parser.parse_args()
print(f”Input file: {args.input_file}”)
print(f”Output directory: {args.output_dir}”)
Here, the widget
parameter specifies the type of widget to use for input.
Dropdown Menus:
You can use Gooey to create dropdown menus for certain parameters:
@Gooey
def main():
parser = GooeyParser(description=”Gooey script with dropdown menu”)
parser.add_argument(
‘color’,
help=’Select a color:’,
choices=[‘Red’, ‘Green’, ‘Blue’],
widget=’Dropdown’
)
args = parser.parse_args()
print(f”Selected color: {args.color}”)
Checkbox and Radio Button Groups:
Gooey supports checkboxes and radio button groups:
@Gooey
def main():
parser = GooeyParser(description=”Gooey script with checkboxes and radio buttons”)
parser.add_argument(
‘ — verbose’,
action=’store_true’,
help=’Enable verbose mode’
)
parser.add_argument(
‘ — level’,
choices=[‘Low’, ‘Medium’, ‘High’],
default=’Medium’,
help=’Select logging level’,
widget=’RadioGroup’
)
args = parser.parse_args()
print(f”Verbose mode: {args.verbose}”)
print(f”Logging level: {args.level}”)
Advanced Configuration:
Gooey allows for more advanced configuration options, such as specifying layout options, styling, and more. Check the Gooey documentation for detailed information.
@Gooey(
advanced=True,
default_size=(500, 300),
language_dir=’./languages’,
program_name=”Advanced Gooey Script”
)
def main():
# …
Running from Command Line:
Even with Gooey, you can still run your script from the command line with the specified arguments:
python gooey_script.py input1_value input2_value