October 9, 2015

Make Pretty GUI Apps Fast with Python-Qt - page 3

A Minimal Window, Signals and Slots

  • January 28, 2010
  • By Akkana Peck

Fortunately, QLabel can display images as well as names. So that's the first change: for each suit, make a "pixmap" to display an image. Here are some images you can download with a right-click and "Save image as..."





Back in PokerWindow.__init__, prepare the images like this:

        # Images for the suits:
self.suitpics = ["diamond.png", "spade.png",
"heart.png", "club.png" ]
self.suitpixmaps = [None, None, None, None]

# Read in the suit pixmaps
for i in range(0, len(self.suitpics)) :
self.suitpixmaps[i] = QPixmap(self.suitpics[i])

Use them like this, checking to make sure each image got set correctly:

    def dealNewHand(self) :
for i in range(0, 5) :
rank = random.randint(0, 12);

suit = random.randint(0, 3);
if self.suitpixmaps[suit].isNull() :
else :

Much better! (Figure 4.)

figure 4
figure 4

But it would look better still if the suit names were boldface, twice as big and centered. That's easy to fix with a QFont and some alignment:

        self.font = QFont("", 0, QFont.Bold, False)
self.font.setPointSize(self.font.pointSize() * 2)


        # Create the labels for the card ranks and suits:
for i in range(0, len(self.ranks)) :
self.ranks[i] = QLabel()

Hey, I like this code a lot better (Figure 5) -- I got a full house!

figure 5
figure 5



Python-Qt isn't as well documented as one might hope, and you'll see a lot of out-of-date examples on the web that no longer work. The links I've found most helpful are:

Also try a web search for qt widget gallery -- there are quite a few sites with collections of screenshots so you can find out what all those widgets really look like.


Akkana Peck is a freelance programmer and writer and the author of Beginning GIMP: From Novice to Professional. You can download a more complete example of the Python-Qt poker game from her blog.


Sitemap | Contact Us