#!/usr/bin/python from PyQt4.QtCore import * from PyQt4.QtGui import * import random class PokerWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.mainWidget = QWidget(self) # dummy to contain the layout manager self.setCentralWidget(self.mainWidget) self.grid = QGridLayout() self.grid.columnCount = 5 self.grid.setSpacing(20) self.mainWidget.setLayout(self.grid) self.setWindowTitle('Poker') # The five cards: self.ranks = [None, None, None, None, None] self.suits = [None, None, None, None, None] self.checkboxes = [None, None, None, None, None] # The suits: self.suitpics = ["suits/diamond.png", "suits/spade.png", "suits/heart.png", "suits/club.png" ] self.suitnames = ["Diamonds", "Spades", "Hearts", "Clubs"] self.suitpixmaps = [None, None, None, None] # Names for the ranks: self.ranknames = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" ] self.font = QFont("", 0, QFont.Bold, False) self.font.setPointSize(self.font.pointSize() * 2) menubar = self.menuBar() file = menubar.addMenu('&File') dealmenu = QAction('Deal New Hand', self) dealmenu.setShortcut('Ctrl+D') dealmenu.setStatusTip('Deal a new hand') self.connect(dealmenu, SIGNAL('triggered()'), self.dealNewHand) file.addAction(dealmenu) dealmenu = QAction('Second round', self) dealmenu.setStatusTip('Deal cards for the second round') self.connect(dealmenu, SIGNAL('triggered()'), self.dealRound) file.addAction(dealmenu) exit = QAction('Exit', self) exit.setShortcut('Ctrl+Q') exit.setStatusTip('Exit application') self.connect(exit, SIGNAL('triggered()'), SLOT('close()')) file.addAction(exit) # 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) self.grid.addWidget(self.ranks[i], 0, i) self.suits[i] = QLabel("") self.grid.addWidget(self.suits[i], 1, i) self.checkboxes[i] = QCheckBox() self.grid.addWidget(self.checkboxes[i], 2, i, 1, 1, Qt.AlignCenter) # Read in the suit pixmaps for i in range(0, len(self.suitpics)) : self.suitpixmaps[i] = QPixmap(self.suitpics[i]) # "Deal" button self.dealbutton = QPushButton("Deal") self.grid.addWidget(self.dealbutton, 3, 0, 1, 2) self.connect(self.dealbutton, SIGNAL('clicked()'), self.dealNewHand) # Quit button quitbutton = QPushButton("Quit") self.grid.addWidget(quitbutton, 3, 4) self.connect(quitbutton, SIGNAL('clicked()'), self, SLOT('close()')) 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]) self.checkboxes[i].setCheckState(Qt.Unchecked) # Change the button to deal a second round instead of a new hand self.dealbutton.setText("Deal second round") self.disconnect(self.dealbutton, SIGNAL('clicked()'), self.dealNewHand) self.connect(self.dealbutton, SIGNAL('clicked()'), self.dealRound) def dealRound(self) : for i in range(0, 5) : if not self.checkboxes[i].isChecked() : continue self.checkboxes[i].setCheckState(Qt.Unchecked) 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]) # Change the button to deal a new hand self.dealbutton.setText("Deal new hand") self.disconnect(self.dealbutton, SIGNAL('clicked()'), self.dealRound) self.connect(self.dealbutton, SIGNAL('clicked()'), self.dealNewHand) if __name__ == '__main__': import sys app = QApplication(sys.argv) win = PokerWindow() win.dealNewHand() win.show() app.exec_()