An Intro to EasyGUI_Qt

I recently came across a fun little project called EasyGUI_Qt. It's basically the same thing as EasyGUI except that it uses PyQt4 instead of Tkinter. The idea behind both of these packages is to allow the developer to ask the user simple questions using dialogs.

In this article, we'll spend some time learning how to use this package by looking at a few examples. Note that EasyGUI_Qt works with both Python 2 and 3, although its primary target is Python 3. The documentation states that there may be some issues with unicode in Python 2, but other than that, the widgets should work fine.


Getting Started

You will need to make sure you have a copy of PyQt4 installed before you install can use this module. Once you have that installed, you can use pip (or easy_install) to install EasyGUI_Qt:

pip install easygui_qt

Now that you have that installed, we can learn how to actually do something!


Creating a Message Box

easy_qt_msg

Creating a simple message box to tell the user something is really quite easy. Let's tell them something about Python:

import easygui_qt as easy
easy.set_font_size(18)
easy.show_message(message="Python rocks!", title="EasyGUI_Qt")

Here we import easygui_qt, set the font to 18 points and show our message. Note that the font size change will affect the text in the dialog and the text on the button.


Getting a String

easy_qt_get_string

Asking the user a question is also very easy. Let's take a look:

>>> import easygui_qt as easy
>>> answer = easy.get_string("Do you like Python?", title="Question")
>>> answer
'Yes!'

That was pretty easy too. Let's find out how to get a color from the user.


Getting a Color

easy_qt_color

EasyGUI_Qt actually provides two ways to get the color. You can get the hex value or the rgb value. The dialog looks the same in both cases. Here's how it works:

>>> import easygui_qt as easy
>>> easy.get_color_hex()
'#ffffff'

If you want to get the rgb value, then you would call get_color_rgb instead.


Getting a Username / Password

easy_qt_user_pass

This is probably one of the nicest ways to get authentication information from the user. EasyGUI_Qt allows us to show a dialog to get the user's username and password. Here's the code:

>>> import easygui_qt as easy
>>> easy.get_username_password(title="Authentication Required")
OrderedDict([('User name', ''), ('Password', '')])

Note the it returns an OrderedDict. The last thing we'll look at is how to run EasyGUI_Qt's demos.


Running the EasyGUI_Qt Demos

easy_qt_demo

We'll start by running the main demo which allows you to see all the dialogs you can show the user and what they return. Let's take a look:

>>> from easygui_qt.demos import launcher
>>> launcher.main()

EasyGUI_Qt also provides a little guessing game demo. Here's how to run it:

>>> from easygui_qt.demos.guessing_game import guessing_game
>>> guessing_game()

This will display a series of dialogs that demonstrate a few of the package's dialogs. It's kind of fun, but pretty short.


Wrapping Up

At this point, you should be able to use EasyGUI_Qt. This package is useful for console programs where you want to get information from the user using a GUI instead of just console prompts. If you wanted to write a full-fledged GUI, then you should take a look at PyQt itself or wxPython.


Related Reading

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary