Friday, December 18, 2009

quidgets.prompts ... gtk.Dialogs now one-liners!

As I mentioned in a previous post, I became a tad enraged at PyGtk when I was providing some simple "get a value from a user code" to one of my bughugger collaborators. My response was to write a bit of code to provide a fun and easy way to get values from users, meet the quidgets.prompts namespace.

The essential patter is quite simple. All of the prompts showed above follow the same pattern. To use a prompt, simply:
  1. Call the right function, such as quidgets.prompts.string()
  2. Check the response from the function call to see if the user said "Ok" or not.
  3. If the response was ok, use the returned value.

The code to get a string looks like this:
reponse, val = quidgets.prompts.string()
if reponse == gtk.RESPONSE_OK:
print val
All of the prompts can work just like this, without any arguments in the function. Right now there is:
  1. quidgets.prompts.string()
  2. quidgets.prompts.date()
  3. quidgets.prompts.integer()
  4. quidgets.prompts.price()
  5. quidgets.prompts.decimal()
Typically, though, you'll want to provide users with a bit of context. Each prompt function also supports an argument for a window title, some prompt text for the user, and a default value.

So to create a string prompt, you are more likely to do something like this:
reponse, val = quidgets.prompts.string("A Title","Some prompt text", "some default text")
if reponse == gtk.RESPONSE_OK:
print val


Note that the default value needs to be a type expected by the prompt. quidgets.prompts.date() expects the default value to be a three tuple in the form of year, month (zero indexed!), and day. So it might look like this:
reponse, val = quidgets.prompts.date("Change Birthday","Set your birthday", (1995,06,11))
if reponse == gtk.RESPONSE_OK:
print val


Of course the numeric prompts (integer, price, decimal) all expect numbers for default values.

Speaking of numeric prompts, as you can see, they use spinners to collect input from users. By default the spinners allow negative numbers. If you don't want to allow negative numbers, the numeric prompts have extended the arguments a bit so that you can set the minimum value to zero:
min_val = 0
default_val = 0
reponse, val = quidgets.prompts.price("Enter Price","Set price in dollars between zero and 100:",default_val,min_val)
if reponse == gtk.RESPONSE_OK:
print val


Note that the "lower" button is disabled because the value is at the specified minimum.

You can set the max value and how much you want the spinner to increment with each button click ("step") as well:
min_val = 0
default_val = 50
step = 1
max_val = 100
reponse, val = quidgets.prompts.price("Enter Price","Set price in dollars between zero and 100:",default_val,min_val,step,max_val)
if reponse == gtk.RESPONSE_OK:
print val

These guys seem ready to go. Next up, I'll be adding prompts to manage asynchronous activity. So that you can create a prompt to display while a long running computation is occurring without freezing the UI. Stay tuned.

About Quidgets
Quickly + Widgets = Quidgets
There is a Launchpad Project for Quidgets
The most up to date changes are in the Quidgets Trunk Branch
You can install Quidgets from the my PPA

3 comments:

  1. Nifty stuff. Would it possible to have the buttons follow the GNOME HIG and have verbs on them?

    ReplyDelete
  2. Sounds like Zenity for Python.

    ReplyDelete