Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyQt] Pixmap Dosnt always load
#1
So, im trying to create a video poker training program in PyQt5 and im using button with Pixmap for the cards. The issue im running into in testing the deal function is that when i try to draw the cards, sometimes the cards appear, sometime 1 or 2 will show up blank like it didnt load, sometimes the card is incorrect or a duplicate of the last card. Im not sure if its an issue with the gui thread and how it processes.

Any help would be appreciated.

İmage


İmage


from PyQt5 import QtCore, QtGui, QtWidgets
import random, time

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(926, 773)
        self.card1 = QtGui.QIcon()
        self.card2 = QtGui.QIcon()
        self.card3 = QtGui.QIcon()
        self.card4 = QtGui.QIcon()
        self.card5 = QtGui.QIcon()
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(1, 7, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(1, 7, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(1, 7, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(1, 7, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        MainWindow.setPalette(palette)
        MainWindow.setDockOptions(QtWidgets.QMainWindow.AllowTabbedDocks | QtWidgets.QMainWindow.AnimatedDocks)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 370, 185, 269))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
        self.pushButton.setPalette(palette)
        self.pushButton.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.pushButton.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.pushButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Card Back 3.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton.setIcon(icon)
        self.pushButton.setIconSize(QtCore.QSize(400, 267))
        self.pushButton.setFlat(False)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(190, 370, 185, 269))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
        self.pushButton_2.setPalette(palette)
        self.pushButton_2.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.pushButton_2.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.pushButton_2.setText("")
        self.pushButton_2.setIcon(icon)
        self.pushButton_2.setIconSize(QtCore.QSize(400, 267))
        self.pushButton_2.setFlat(False)
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(370, 370, 185, 269))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
        self.pushButton_3.setPalette(palette)
        self.pushButton_3.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.pushButton_3.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.pushButton_3.setText("")
        self.pushButton_3.setIcon(icon)
        self.pushButton_3.setIconSize(QtCore.QSize(400, 267))
        self.pushButton_3.setFlat(False)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(550, 370, 185, 269))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
        self.pushButton_4.setPalette(palette)
        self.pushButton_4.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.pushButton_4.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.pushButton_4.setText("")
        self.pushButton_4.setIcon(icon)
        self.pushButton_4.setIconSize(QtCore.QSize(400, 267))
        self.pushButton_4.setFlat(False)
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(730, 370, 185, 269))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
        self.pushButton_5.setPalette(palette)
        self.pushButton_5.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.pushButton_5.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.pushButton_5.setText("")
        self.pushButton_5.setIcon(icon)
        self.pushButton_5.setIconSize(QtCore.QSize(400, 267))
        self.pushButton_5.setFlat(False)
        self.pushButton_5.setObjectName("pushButton_5")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(10, 11, 906, 311))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(2, 0, 145))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(2, 0, 145))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 96, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        self.tableWidget.setPalette(palette)
        self.tableWidget.setFrameShape(QtWidgets.QFrame.Panel)
        self.tableWidget.setFrameShadow(QtWidgets.QFrame.Plain)
        self.tableWidget.setLineWidth(3)
        self.tableWidget.setRowCount(6)
        self.tableWidget.setColumnCount(6)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.horizontalHeader().setVisible(False)
        self.tableWidget.verticalHeader().setVisible(False)
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(418, 670, 90, 62))
        self.pushButton_6.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("Denom25c.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_6.setIcon(icon1)
        self.pushButton_6.setIconSize(QtCore.QSize(247, 63))
        self.pushButton_6.setFlat(True)
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_7.setGeometry(QtCore.QRect(280, 680, 131, 41))
        self.pushButton_7.setText("")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("BLANK3.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_7.setIcon(icon2)
        self.pushButton_7.setIconSize(QtCore.QSize(222, 39))
        self.pushButton_7.setFlat(True)
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_8.setGeometry(QtCore.QRect(146, 680, 131, 41))
        self.pushButton_8.setText("")
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap("MOREGAMES5.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_8.setIcon(icon3)
        self.pushButton_8.setIconSize(QtCore.QSize(222, 39))
        self.pushButton_8.setFlat(True)
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_9.setGeometry(QtCore.QRect(10, 680, 131, 41))
        self.pushButton_9.setText("")
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap("GAMEINFO2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_9.setIcon(icon4)
        self.pushButton_9.setIconSize(QtCore.QSize(222, 39))
        self.pushButton_9.setFlat(True)
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_10.setGeometry(QtCore.QRect(513, 680, 131, 41))
        self.pushButton_10.setText("")
        self.pushButton_10.setIcon(icon2)
        self.pushButton_10.setIconSize(QtCore.QSize(222, 39))
        self.pushButton_10.setFlat(True)
        self.pushButton_10.setObjectName("pushButton_10")
        self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_11.setGeometry(QtCore.QRect(648, 680, 131, 41))
        self.pushButton_11.setText("")
        icon5 = QtGui.QIcon()
        icon5.addPixmap(QtGui.QPixmap("BETUP.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_11.setIcon(icon5)
        self.pushButton_11.setIconSize(QtCore.QSize(222, 39))
        self.pushButton_11.setFlat(True)
        self.pushButton_11.setObjectName("pushButton_11")
        self.pushButton_12 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_12.setGeometry(QtCore.QRect(783, 680, 131, 41))
        self.pushButton_12.setText("")
        icon6 = QtGui.QIcon()
        icon6.addPixmap(QtGui.QPixmap("DRAW2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_12.setIcon(icon6)
        self.pushButton_12.setIconSize(QtCore.QSize(222, 39))
        self.pushButton_12.setFlat(True)
        self.pushButton_12.setObjectName("pushButton_12")
        self.labelc1 = QtWidgets.QLabel(self.centralwidget)
        self.labelc1.setGeometry(QtCore.QRect(10, 370, 185, 269))
        self.labelc1.setText("")
        self.labelc1.setObjectName("labelc1")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 926, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.cards = ['AHEART.png','KHEART.png','QHEART.png','JHEART.png','10HEART.png','9HEART.png','8HEART.png','7HEART.png','6HEART.png','5HEART.png','4HEART.png','3HEART.png','2HEART.png',
                      'ACLUB.png','KCLUB.png','QCLUB.png','JCLUB.png','10CLUB.png','9CLUB.png','8CLUB.png','7CLUB.png','6CLUB.png','5CLUB.png','4CLUB.png','3CLUB.png','2CLUB.png',
                      'ADIAMIND.png','KDIAMIND.png','QDIAMIND.png','JDIAMIND.png','10DIAMIND.png','9DIAMIND.png','8DIAMIND.png','7DIAMIND.png','6DIAMIND.png','5DIAMIND.png','4DIAMIND.png','3DIAMIND.png','2DIAMIND.png',
                      'ASPADE.png','KSPADE.png','QSPADE.png','JSPADE.png','10SPADE.png','9SPADE.png','8SPADE.png','7SPADE.png','6SPADE.png','5SPADE.png','4SPADE.png','3SPADE.png','2SPADE.png',]
        self.pushButton_12.clicked.connect(self.picswitchtest)
        self.cardtimer = QtCore.QTimer()
        self.cardtimer.setSingleShot(True)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def picswitchtest(self):
        self.rancard1()

        #timer.timeout.connect(self.rancard2)
        #timer.start(500)


    def rancard1(self):
        print('test')
        card1 = random.choice(self.cards)
        print(card1)
        self.card1.addPixmap(QtGui.QPixmap(card1))
        self.pushButton.setIcon(self.card1)
        self.cardtimer.singleShot(1000, self.rancard2)
        #self.cardtimer.start(1000)

    def rancard2(self):
        card2 = random.choice(self.cards)
        print(card2)
        self.card2.addPixmap(QtGui.QPixmap(card2), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_2.setIcon(self.card2)
        self.cardtimer.singleShot(1000, self.rancard3)
        #self.cardtimer.start(1000)

    def rancard3(self):
        card3 = random.choice(self.cards)
        print(card3)
        self.card3.addPixmap(QtGui.QPixmap(card3), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(self.card3)
        self.cardtimer.singleShot(1000, self.rancard4)
        #self.cardtimer.start(1000)

    def rancard4(self):
        card4 = random.choice(self.cards)
        print(card4)
        self.card4.addPixmap(QtGui.QPixmap(card4), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_4.setIcon(self.card4)
        self.cardtimer.singleShot(1000, self.rancard5)
        #self.cardtimer.start(1000)

    def rancard5(self):
        card5 = random.choice(self.cards)
        print(card5)
        self.card5.addPixmap(QtGui.QPixmap(card5), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_5.setIcon(self.card5)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Quote
#2
You might want to create all the pixmap objects on init and store them in a list (self.cards), so QPixmap() will be called only once on each image. Then try creating fresh QIcon instance by loading the pixmap directly:

def rancard1(self):
	pixmap = random.choice(self.cards)
	self.card1 = QtGui.QIcon(pixmap)
	print(pixmap.isNull())  # Debugging
	...

Hope this helps
Quote
#3
Thank you for the suggestion. I made the following changes and its still doing it. Almost worse sometimes it seems. Did I do it correctly?

        self.cardfiles = ['AHEART.png','KHEART.png','QHEART.png','JHEART.png','10HEART.png','9HEART.png','8HEART.png','7HEART.png','6HEART.png','5HEART.png','4HEART.png','3HEART.png','2HEART.png',
                      'ACLUB.png','KCLUB.png','QCLUB.png','JCLUB.png','10CLUB.png','9CLUB.png','8CLUB.png','7CLUB.png','6CLUB.png','5CLUB.png','4CLUB.png','3CLUB.png','2CLUB.png',
                      'ADIAMIND.png','KDIAMIND.png','QDIAMIND.png','JDIAMIND.png','10DIAMIND.png','9DIAMIND.png','8DIAMIND.png','7DIAMIND.png','6DIAMIND.png','5DIAMIND.png','4DIAMIND.png','3DIAMIND.png','2DIAMIND.png',
                      'ASPADE.png','KSPADE.png','QSPADE.png','JSPADE.png','10SPADE.png','9SPADE.png','8SPADE.png','7SPADE.png','6SPADE.png','5SPADE.png','4SPADE.png','3SPADE.png','2SPADE.png',]
        self.pushButton_12.clicked.connect(self.picswitchtest)
        self.cardtimer = QtCore.QTimer()
        self.cardtimer.setSingleShot(True)
        self.cards = []
        for items in self.cardfiles:
            self.cards.append(QtGui.QPixmap(items))

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def picswitchtest(self):
        self.rancard1()

    def rancard1(self):
        print('test')
        c1pixmap = random.choice(self.cards)
        self.card1 = QtGui.QIcon(c1pixmap)
        print(c1pixmap.isNull())
        self.pushButton.setIcon(self.card1)
        self.cardtimer.singleShot(500, self.rancard2)

    def rancard2(self):
        c2pixmap = random.choice(self.cards)
        self.card2 = QtGui.QIcon(c2pixmap)
        print(c2pixmap.isNull())
        self.pushButton_2.setIcon(self.card2)
        self.cardtimer.singleShot(500, self.rancard3)

    def rancard3(self):
        c3pixmap = random.choice(self.cards)
        self.card3 = QtGui.QIcon(c3pixmap)
        print(c3pixmap.isNull())
        self.pushButton_3.setIcon(self.card3)
        self.cardtimer.singleShot(500, self.rancard4)

    def rancard4(self):
        c4pixmap = random.choice(self.cards)
        self.card4 = QtGui.QIcon(c4pixmap)
        print(c4pixmap.isNull())
        self.pushButton_4.setIcon(self.card4)
        self.cardtimer.singleShot(500, self.rancard5)

    def rancard5(self):
        c5pixmap = random.choice(self.cards)
        self.card5 = QtGui.QIcon(c5pixmap)
        print(c5pixmap.isNull())
        self.pushButton_5.setIcon(self.card5)
Quote
#4
I think i figured out the issue. When iterated through the objects and checked if they were null, it returned null for all the Diamonds because i spelled all the names wrong i copy and pasted DIAMID instead of DIAMOND......

Now none of them show up false.

While your answer didnt directly solve my problem since you dont have the png files, it showed me how to put objects in a list and then check if they are null which lead me to my answer. Thank you so much!
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)