I am currently reading through Mark Summerfield’s book on PyQt, Rapid GUI Programming with Python and Qt and thought it would be fun to take some of the example applications in it and convert them to PySide. So I’ll be creating a series of articles where I’ll show the original PyQt examples from the book and then convert them to PySide and probably add something of my own to the code. The book doesn’t really get in Qt GUI coding until chapter 4 where the author creates a fun little currency converter. Come along and enjoy the fun!

If you do that and save it, you’ll be done porting and the code will now work in PySide land. However, I think we need to go a bit further and make this code a little smarter. You’ll notice in the original that every time you run it, the application will go online and download the exchange rate data. That seems to be a little bit of overkill, so let’s add a check to see if it’s already downloaded and less than a day old. Only if it’s older than a day will we want to download a new copy. And instead of using urllib2, let’s use the new-fangled requests library instead.

Okay, let’s spend a little time looking at the code. We won’t explain everything, but we’ll go over what was changed. First off, we imported some more modules: datetime, os, requests and sys. We also imported PySide itself so we could easily add its version information to the dialog’s title bar. Next we added a couple of new methods: downloadFile and processLine. But first we need to look at what was changed in getdata. Here we grab the path that the script is running in and use that to create a fully qualified path to the rates file. Then we check if it already exists. If it doesn’t exist, we call the downloadFile method which uses the requests library to download the file. We’re also using Python’s with context manager construct which will automatically close the file when we’re done writing to it.

If the file DOES exist, then we fall to the else part of the statement in which we check if the file’s last modified date is older than today’s date. If it is, then we download the file and overwrite the original. Then we move on to processing the file. To do the processing, we move most of the code into a separate method called processLine. The primary reason for doing so is that if we don’t, we end up with a very complicated nested structure that can be hard to follow. We also add a check to see if the date has been returned from processLine and if it has, we set the date variable. The rest of the code stayed the same.

Wrapping Up

At this point, you should know how to create a very simple PySide application that can actually do something useful. Not only that, but you also have a very general idea of how to port your PyQt code to PySide. You have also been exposed to two different ways of downloading files from off the web. I hope this has opened your eyes to the power of Python and the fun of programming.

Note: The code in this article was tested on Windows 7 Professional with Python 2.6.6, PySide 1.2.2 and PyQt4

I’ve re-subscribed to your blog after deciding to finally settle on Python as my implementation language, but it’s interesting that you’re working on the same book I’ll do.

I’m not sure if wxWidgets-3.0 will be released ever, so I gave up on wx(python) and decided to use Qt, but still not sure whether PyQt or PySide (we’ll do open-source project, so license is not the issue).

In any case, nice to be here again, this time learning from you about Python GUI programming using Qt.

Jaz

i need help can somebody good with python get to me really quickly on my e-mail please… lowkeyjaz@live.co.uk its really really important as im a beginner on python

Eric

This is helpful, wish I had seen it before, as I’m in the process of translating all the examples from his book into PySide (with new-style signals and slots). You can find it here: https://github.com/EricThomson/PySideSummer