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.
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.
- 1Linux Top 3: GNOME 3.12 and New Betas for Ubuntu 14.04 and OpenMandriva Lx 2014.0
- 2Linux Top 3: Linus Lashes out, Linux 3.14 Gets PIE and Ubuntu One is Done.
- 3Linux Top 3: Ubuntu 14.04, Debian Gives Squeeze More Life and Red Hat Goes Atomic
- 4Linux Top 3: RHEL 6.5, Debian 7.2 and EOL for Linux 3.0.x
- 5Linux Top 3: CoreOS, Oracle Enterprise Linux 7 and Ubuntu 14.10