A quick and simple GUI in Python

A quick and simple GUI in Python

Python is an easy-to-learn but powerful language, and comes with a wealth of libraries. It is often used to create simple scripts. You run these on the command line, giving it any parameters it needs.

Remembering the parameters can be tricky. A single library and a few lines of code turn the manual parameter entry into a graphical user interface. The app shown above took just 10 lines of code.

import random
import gooey


@gooey.Gooey(default_size=(300, 300), show_success_modal=False)
def main():
    parser = gooey.GooeyParser(description='Role some dice')
    parser.add_argument('number_of_rolls', type=int, 
      metavar='How many rolls?', choices=range(1, 10))
    args = parser.parse_args()
    for i in range(args.number_of_rolls):
        print(f'Roll {i + 1}: {random.randint(1, 6)}')


main()

For more information, including a line by line explanation of the code below, see the accompanying blog post

To view or add a comment, sign in

More articles by Coen de Groot

  • Python - Count and learn

    Here is a simple problem: Count how many times each character occurs in a string. And one more thing: Do this using…

    14 Comments
  • Python support - Ask wisely, ask nicely

    What do you do if you need some Python support, from a quick answer to affordable regular mentoring or tutoring? Python…

    2 Comments
  • Control your computer using Python

    Python is an excellent language for taking control of your computer and performing some of the more tedious tasks. I…

  • Playful Python - Simple recursion with Python Turtles

    Python is a simple but powerful language, and comes with a wealth of libraries. The black line of the image above took…

  • Practical Python - A plot in your browser with Bokeh

    Python is a simple but powerful language, and comes with a wealth of libraries. The chart above took just 9 lines of…

  • Practical Python - Updating spreadsheets

    How would you like to grab a share price daily, and store it in a spreadsheet? Or add a new column to dozens of…

  • Practical Python - Easy fractions

    When you ask your spreadsheet to calculate 1/2 + 1/3 you get something like this: This is obviously an approximation…

    1 Comment
  • Practical Python in 10 lines - Retrieve and display a data set

    Python is a simple but powerful language, and comes with a wealth of libraries. The chart above took just 10 lines of…

Explore content categories