The Dialogs of wxPython (Part 2 of 2)

For the second half of this series, I discovered that there are even more dialogs than I originally thought. While it would have probably been a good idea to have split this into three parts, we’re going to stick with just two. In this article, we’re going to cover the following dialogs:

GenericMessageDialog (AGW)

ImageDialog

wx.MultiChoiceDialog

wx.PageSetupDialog

wx.PrintDialog

wx.ProgressDialog

PyBusyInfo (AGW)

PyProgress (AGW)

ScrolledMessageDialog

wx.SingleChoiceDialog

wx.TextEntryDialog

For the uninitiated, there’s also an AboutBox dialog that’s not here for the very simple reason that it’s already been covered in this blog outside of this series. Check that out for yourself. Just to clear up any confusion about why wx.Dialog isn’t here: this series ONLY covers pre-built dialogs. The wx.Dialog widget is great for creating your own custom dialogs. The last note that should be covered is that the example code here has been yanked from the wxPython demo and re-purposed for this article.

Now, on with the show!

The GenericMessageDialog

The GenericMessageDialog is a part of the AGW library of generic controls created by Andrea Gavana. It gives us more options than the MessageDialog itself did because the GenericMessageDialog is written in pure python and is therefore, much more hackable. Let’s see how we can create the dialog in the screenshot:

The first thing to look for in any example is what is imported at the top of the file. In this case, we use the images.py file from the wxPython demo to supply some of the images in this example. We also import the dialog that we need from wx.lib.agw.genericmessagedialog. We can skip the frame’s initialization code since we want to focus on the dialog. The first dialog related method we see is SetProperties. It sets up a few default options in the dialog, like checking the “ok” checkbox, selecting the first radio button and putting the focus on the button that will open the generic dialog. The first two will tell our dialog creator method, OnShowDialog, what kind of dialog we want. Thus, our next method to look at is the OnShowDialog one.

Here we find that the GenericMessageDialog takes mostly the same values that a normal MessageDialog would, namely a parent, message, title caption, and some styles. You can also set its size, pos and agwstyle when applicable. We can also add an icon to the dialog by calling SetIcon. There you have it! A generic message dialog!

View Images with ImageDialog

The ImageDialog is a handy little dialog that you can use to preview various image formats. This dialog is another generic dialog, but it’s not part of the AGW library. The ImageDialog is quite handy and super easy to create. How easy? Well check out the code below and see for yourself!

If you look in the onButton method, you can see how we initialize this dialog. Basically all you need to feed the control is a parent and a directory full of images, then show the dialog. That’s it! Didn’t I tell you it was easy?

Making Choices with wx.MultiChoiceDialog

What do you do if you need to ask the user for one or more pieces of simple information? You can create your own control or you might choose the wx.MultiChoiceDialog. The MultiChoiceDialog provides you the ability to serve up a dialog that allows the user to choose multiple choices. Yes, it is pretty obvious…so on to the code!

The MultiChoiceDialog is like most dialogs. It accepts a parent, descriptive label, window caption and a list of choices. To get the user’s selections, all you need to do is call the dialog’s GetSelections method. That’s it!

wx.PageSetupDialog

The wx.PageSetupDialog is used for setting up a few parameters for printing out a page or just setting up a page for editing. You can use it to adjust paper size, margins and whether or not the page should be in landscape or portrait orientations. Here’s how you set one up:

Let’s take a gander at the onButton method. First we need to set up some defaults for the dialog, which is what the first four lines are for. Next we need to get the user’s choices, so in the conditional IF statement we see how to access a few of the settings in the dialog, such as GetMarginTopLeft or GetMarginBottomRight. You can get the other bits of information using other getters like GetPaperSize. Note that to get access to these methods you first have to create an object based on GetPageSetupData.

wx.PrintDialog

The wx.PrintDialog allows the user to choose what printer to print to and give the user the normal options that he or she would see if they were to try to print something. It’s a handy wrapper around the platform’s native print dialog. Here’s how you make one:

Much like the PageSetupDialog, we need to set up some defaults for this dialog too. Here we set up the min and max page numbers, enable print to file, etc. When you grab the user’s choices, you might want to save them and instantiate the dialog with that information so they don’t have to re-customize their print job should they want to print it again. Anyway, to actually get the user’s choices, we call the dialog’s GetPrintDialogData method and then use the result to query the other pieces. Our example just shows how to get all the pages that will print. You can drill down to more information by doing the following:

You’ll have to refer to the documentation for wxPython, wxWidgets or maybe even your platform to figure out what some of the results mean.

wx.ProgressDialog

The wx.ProgressDialog can be used to show the user how far along a download is or give the user feedback about some other long running process. You could also use the wx.Gauge or PyProgress (AGW) to give the user similar information. We’ll look at that soon, but first we’re going to check out how to create the wx.ProgressDialog:

As you can see, the ProgressDialog accepts the following arguments: window caption, message, amount of time to run, parent, and various styles. We added the wx.PD_CAN_ABORT style to allow us to abort the dialog. The rest of the styles and methods are pretty self-explanatory.

PyBusyInfo (AGW)

In the previous article, we covered the BusyDlg. Well, Andrea Gavana went and wrote a pure python version of that virtually unknown dialog that gives us the ability to add an image! And since it’s python, we can add other features to it quite easily. Here’s how you create one:

Note that the PyBusyInfo also has an oddball method of destroying the dialog. In this case, we call python’s builtin, “del”. Hopefully Mr. Gavana will add a Destroy() method to it so the dialog can be destroyed in the standard way. Also notice how we add the icon by just passing it in with the icon parameter.

PyProgress (AGW)

The PyProgress dialog comes to us courtesy of the industrious Andrea Gavana. Yes, it’s another AGW library widget, which means it’s also pure python. It looks a little different than the wx.ProgressDialog. Hopefully you’ll like its aesthetics too. Let’s find out how to make one of our own:

The code above is pretty much like all the other dialogs we’ve seen so far. Just for fun, I’ll let you figure out how this one works.

ScrolledMessageDialog

The ScrolledMessageDialog is one of the few generic dialogs that Andrea Gavana didn’t write. It’s also one of the easiest to create and use. Don’t believe me? Well, read the code below and see how wrong you are!

As you can clearly see, all you need is some text to pass to the dialog. In this case, we read one of the other python files and passed it to the dialog. I hope you’ve been paying attention to the special imports we’ve had to do for these generic dialogs. For this one, we had to import wx.lib.dialogs to gain access to the ScrolledMessageDialog.

wx.SingleChoiceDialog

Sometimes you want to give the user a list of items, but only allow him to select one. You could use a custom wx.Dialog with a set of radio buttons or you could use the wx.SingleChoiceDialog. In case you didn’t guess, we’re going with the latter…

Here the SingleChoiceDialog accepts most of the same arguments that the other dialogs have taken. Take note that we need to pass a list in to create the list of choices and the wx.CHOICEDLG_STYLE style to make the dialog function correctly. Other than that, we’ve been there, done that.

wx.TextEntryDialog

The wx.TextEntryDialog gives us the ability to allow the user to tell us whatever he wants. Yes, that is scary, but sometimes you just have to do it. You might use this dialog to get non-specific comments or feedback from your venerable users. Here’s how you can do it:

Here we see that the dialogs arguments include the following: parent, message, caption, defaultValue. Of course, we override the defaultValue when we call the dialog’s SetValue() method. The rest of the code should look familiar.

Wrapping up

If you made it this far, congratulations! You just slogged through one of the longest posts ever written on this site! You should now be an expert in wxPython dialogs. If you want even more information, feel free to read the many references below to continue your education. And get out there and start using these dialogs to great effect!

Note: I didn’t use anything other than the wxPython demo, Wingware’s IDE and few documentation pages in writing this article.