To date, I’ve only been able to write little command line programs that I needed for various tasks like getting average latency between offices. Command line utilities are great for sys admins, and they are fine when it’s just something I need personally. As soon as you want to provide a utility to a regular user, you better put a GUI in front of them, or they are going to act like you asked them to play Tank in the Matrix.

The first gui app I built was an expansion of a command line app I wrote for exporting the Exchange Global Address List. The command line app exported it to CSV, which was all I needed for my purposes. I wrote it so I could email an updated email list for a client of mine to their offices in Shanghai, which has it’s own email system.

Having been asked about how to print out the GAL many times over the years, I decided to write a gui app for regular users and give them the ability to pick the fields they wanted. Here’s what the app looks like.

 

So how did I learn to write this? First I watched a couple basic Youtube videos and skimmed through this PyQt4 tutorial. The tutorial was great to give me an idea of how the code functions. It gave me a place to start, although I still felt like I was missing a lot. The best way for me to learn once I have a little understanding is to just jump in and get at it.

Now, I know some people might not like this, but I learned the fastest by jumping into QT Designer. After poking around, I quickly figured out how to draw up my screens.  OK, I have the screen designed. Now what?

I got stuck here for a second until I found out how to convert the file you create with QT Designer into a python file. You do that with the “pyuic4” command. Here is the command I used.

“pyuic4 -x -o pythonfilename.py qtdesignerfilename.ui”

The -x will put a main function in at the end, so you can run the script and get your gui. This isn’t necessary with the way you will eventually want to write your app. More on that later. The -o is to specify your output file name and the last file name is the QT Designer file.

Wow, I can launch the gui now, but guess what? I doesn’t do anything. How do I put code in to make it work. I remember when I learned a little VB back in school. It was simple in VB. Just double click the object and start typing your code. Not so with with Python. Well, back to Youtube in search of how to make buttons, checkboxes, etc work.

After watching a couple videos, I found that you need to put something similar to the following line of code into the function you have that sets up your ui.

Self is referring to the class itself. btnClose is referring to the close button I drew on the gui. Clicked is what action is being taken on the button and connect is what calls the action (function) you want to take. In this case, it’s going to call a function called buttonClicked. Now all you have to do is put the code in the buttonClicked function. I basically used this same line for every button, and then my buttonClicked function checks to see which button was clicked.

In this example, lets say you click the Close button. That calls the buttonClicked function, which will have something similar to the following if statement:

This gets the text of the button and compares it to see if it equals “Close”. If so, the program exits. Seems pretty simple right.

Once you see how to do something for one object, it’s easier to figure out the other ones. Two invaluable resources are the list of PyQt classes and the list of PySide classes. Using these two sites, you can pretty much figure out how to change objects, how to read their properties, etc.

One example of reading objects properties would be the check boxes for the fields in the above screen. All I did (not sure it’s the best way) was check the value of isChecked for each of the checkboxes when the Export button was clicked. Using if statements to see if they were checked, I built a list of the fields the user requested. Here’s the code.

I’m sure you get the idea. Now,let’s get back to what I was saying about not needing the execution code at the bottom of the python file. You do not necessarily need that other than to test out what the gui looks like. When you actually write your app, you’ll want most of your logic in a separate python file. To get the gui, you’ll import the python file you made with pyuic4. The reason you’ll want to do this is in case you want to change your gui. If you change your gui and run pyuic4 again, you are going to lose all your code. By keeping your logic code in a separate file, you can change the gui and run pyuic4 until your hearts content.

One last thing you are probably wondering is what’s the difference betwen PyQt and PySide. From what I understand, it comes down to licensing. I don’t think I’m knowledgeable enough to talk about functionality. PySide has a much more flexible licensing model. The nice thing is they are very much a like. If you write your app in PyQt and want to change, simply change your import statements, and you should be able to get almost everything to work. I had a few exceptions.

On the second app I wrote, I had password fields. Changing the echo for the password field is different between the two, but not significantly. You just have to hit those two sites I listed above, and you can figure out the differences. I’ll be writing more about the second app here shortly and posting code. You’ll get to see PySide in action.

As always, let me know what you think. I learn a lot by just jumping in, but I’m sure I do a lot of bad things as well.