PyQt无法正确显示像素图

时间:2019-02-04 00:03:02

标签: python qt pyqt pyqt5

所以我是PyQt的新手,我似乎不太能解决所有问题。由于某些原因,每当我单击“玩游戏”按钮时,图像就不会出现。但是,它确实运行InitUI。有人可以告诉我我做错了什么吗? (当它最初只是加载图像时,图像便出现了。

类示例(QMainWindow):

def __init__(self):
    super().__init__()

    self.title = 'PyQt5 image - pythonspot.com'
    self.initUI()

def initUI(self):

    central_widget = QWidget()
    self.chess = ChessWidget(central_widget)
    self.setCentralWidget(central_widget)
    self.setWindowIcon(QIcon('web.png'))
    self.resize(900,900)
    self.center()
    self.setFixedSize(self.size())
    self.show()

def toggleMenu(self, state):

    if state:
        self.statusbar.show()
    else:
        self.statusbar.hide()

# def closeEvent(self, event):
#
#     reply = QMessageBox.question(self, 'Message',
#                                  """Are you sure you want to quit?""", QMessageBox.Yes |
#                                  QMessageBox.No, QMessageBox.No)
#
#     if reply == QMessageBox.Yes:
#         event.accept()
#     else:
#         event.ignore()

def center(self):

    qr = self.frameGeometry()
    cp = QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())

类ChessWidget(QFrame):

def __init__(self, parent):
    super().__init__(parent)
    qbtn = QPushButton('Play Game', self)
    qbtn.clicked.connect(lambda: qbtn.close())
    qbtn.clicked.connect(lambda: self.initUI())
    qbtn.resize(qbtn.sizeHint())

    hbox = QHBoxLayout()
    hbox.addStretch(1)
    hbox.addWidget(qbtn)
    vbox = QVBoxLayout()
    vbox.addStretch(1)
    vbox.addLayout(hbox)
    self.setLayout(vbox)

def initUI(self):
     print("hi")
     pixmap = QPixmap("ChessBoard.jpg")
     lbl = QLabel(self)
     pixmap2 = pixmap.scaledToWidth(900)
     hbox = QHBoxLayout(self)
     hbox.addStretch(1)
     hbox.addWidget(lbl)
     lbl.setPixmap(pixmap2) ` if __name__ == '__main__':
app = QApplication([])
ex = Example()
sys.exit(app.exec_()) `

2 个答案:

答案 0 :(得分:1)

您应该从Qt得到有用的警告;如果不是,请检查您的测试环境是否已激活控制台。警告是:

QLayout: Attempting to add QLayout "" to ChessWidget "", which already has a layout

QHBoxLayout中创建ChessWidget.initUI并尝试将其作为ChessWidget的父元素时,会发生这种情况。您已经在该小部件上设置了QVBoxLayout

一种快速的解决方案是保留布局的名称(vbox-> self.vbox),然后在单击事件中,从布局中删除QPushButton并添加{{1 }}。

我了解您只是出于学习目的而进行小测试,但是这种ChessWidget被永久替换的设计模式可能不是您想要的。如果希望QPushButtonQPushButton占据相同的空间,请查看ChessWidget。这将允许您根据需要从一个小部件切换到另一个小部件。例如,如果您想在以后没有活动的情况下隐藏QStackedWidget,这可能是一种有用的方法。

请注意,在创建ChessWidgetQPushButton时,不必将它们作为QLabel的父项,因为添加后它们将重新放置到布局中。

答案 1 :(得分:0)

尝试一下:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        central_widget = QWidget()
        self.chess     = ChessWidget(central_widget)   
        self.setCentralWidget(central_widget)

        self.layV = QVBoxLayout(central_widget)               # +++
        self.layV.addWidget(self.chess)                       # +++

        self.setWindowIcon(QIcon('D:/_Qt/img/py-qt.png')) # web.png
        self.resize(440,440)    #(900,900)

class ChessWidget(QFrame): 
    def __init__(self, parent=None):
        super().__init__(parent)

        qbtn = QPushButton('Play Game', self)
        qbtn.clicked.connect(lambda: qbtn.close())
        qbtn.clicked.connect(lambda: self.initUI())

        self.hbox = QHBoxLayout()  
        self.hbox.addWidget(qbtn)
        self.vbox = QVBoxLayout()
        self.vbox.addStretch(1)
        self.vbox.addLayout(self.hbox)
        self.setLayout(self.vbox)

    def initUI(self):
        print("hi")
        pixmap = QPixmap("D:/_Qt/img/pyqt.jpg") # ChessBoard.jpg
        lbl    = QLabel(self)
        self.vbox.addWidget(lbl)
        lbl.setPixmap(pixmap.scaled(400, 400, Qt.KeepAspectRatio))  # +++

if __name__ == '__main__':
    app = QApplication([])
    ex = Example()
    ex.setWindowTitle('PyQt5 image - pythonspot.com')
    ex.show()
    sys.exit(app.exec_())

enter image description here