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..."
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() :
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)
# 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!
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.
Sponsored by BlackBerry
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!