Make Pretty GUI Apps Fast with Python-Qt - page 3
A Minimal Window, Signals and Slots
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..."
![[spade]](/imagesvr_ce/linuxplanet/spade.png)
![[diamond]](/imagesvr_ce/linuxplanet/diamond.png)
![[club]](/imagesvr_ce/linuxplanet/club.png)
![[heart]](/imagesvr_ce/linuxplanet/heart.png)
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);
self.ranks[i].setText(self.ranknames[rank])
suit = random.randint(0, 3);
if self.suitpixmaps[suit].isNull() :
self.suits[i].setText(self.suitnames[suit])
else :
self.suits[i].setPixmap(self.suitpixmaps[suit])
Much better! (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)
and
# Create the labels for the card ranks and suits:
for i in range(0, len(self.ranks)) :
self.ranks[i] = QLabel()
self.ranks[i].setFont(self.font)
self.ranks[i].setAlignment(Qt.AlignCenter)
Hey, I like this code a lot better (Figure 5) -- I got a full house!
Links
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:
- PyQt's Classes
- GUI Programming with Python: QT Edition
- The PyQt4 tutorial (which includes a nifty Tetris game)
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.
- Skip Ahead
- 1. A Minimal Window, Signals and Slots
- 2. A Minimal Window, Signals and Slots
- 3. A Minimal Window, Signals and Slots


Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.