python - PyQt5:通过小部件方法在 QAction 之后更新 MainApplication 布局

标签 python python-3.x oop pyqt pyqt5

我正在尝试创建我的第一个内存游戏。

由于我的问题here的回答,我最近发现了如何让它工作。 。工作代码发布在同一个链接中,但现在我试图将小部件和主应用程序类分开,1)因为我认为这样更好,2)因为我想更好地了解 OOP 的工作原理。

所以,我在这里发布我的新代码,除了最后一部分之外,它似乎可以工作。概念:

  1. MainApplication 有一个菜单栏,允许路径到图像文件夹,并实例化 MemoryGame 小部件,该小部件又初始化一个空的 QGridLayout。
  2. 单击"file"-> 在菜单栏中打开,将调用 showDialog 方法。
  3. 选择文件夹后,将调用(MemoryGame 对象的)populate_grid 方法。
  4. populate_grid“应该”用找到的每个图像填充 QGridLayout。
  5. 网格应该显示在屏幕上,但事实并非如此......

我很确定问题出在 populate_grid 的最后一行,我在其中向网格布局添加了元素,但我不知道如何解决该问题。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Memory game 3

My first memory game in PyQt5.

author: Umberto Minora 
last edited: September 2016
"""

import os
import sys
import glob
import math

from PyQt5.QtWidgets import (QMainWindow, QWidget,
    QGridLayout, QPushButton, QApplication,
    QAction, QFileDialog, QLabel)

from PyQt5.QtGui import QPixmap

class MainApplication(QMainWindow):
    """This is the main application.
    All widgets should be put inside it."""
    def __init__(self, widget):
        super().__init__()

        self.widget = widget
        self.initUI()

    def showDialog(self):
        folder = str(QFileDialog.getExistingDirectory(self, "Select Directory",
            '.', QFileDialog.ShowDirsOnly))

        images = glob.glob(os.path.join(folder, '*.jpg'))

        if images:
            self.widget.populate_grid(images)

    def initUI(self):
        self.statusBar()

        openFile = QAction('Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Search image folder')
        openFile.triggered.connect(self.showDialog)

        menubar = self.menuBar()
        self.fileMenu = menubar.addMenu('&File')
        self.fileMenu.addAction(openFile)

        self.setCentralWidget(self.widget)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Memory Game!')
        self.show()

class MemoryGame(QWidget):
    """This is the Memory Game Widget"""
    def __init__(self):
        super().__init__()

        self.gridWidget = QWidget(self)
        self.gridLayout = QGridLayout(self.gridWidget)

    def populate_grid(self, images):
        n_cols = math.ceil(math.sqrt(len(images)))
        n_rows = math.ceil(math.sqrt(len(images)))
        positions = [(i,j) for i in range(n_cols) for j in range(n_rows)]
        for position, img in zip(positions, images):
            if img == '':
                continue
            pixmap = QPixmap(img)
            scaled = pixmap.scaled(pixmap.width()/3, pixmap.height()/3)
            del(pixmap)
            lbl = QLabel(self)
            lbl.setPixmap(scaled)
            self.gridLayout.addWidget(lbl, *position)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MainApplication(MemoryGame())
    sys.exit(app.exec_())

最佳答案

图像未显示的原因是您将 gridWidget 放入 MemoryGame 小部件中,而该小部件本身没有布局。 MemoryGame 小部件实际上应该替换 gridWidget,因此您需要做的就是:

class MemoryGame(QWidget):
    """This is the Memory Game Widget"""
    def __init__(self):
        super().__init__()
        self.gridLayout = QGridLayout(self)

我还认为您创建 MemoryGame 小部件的方式不必要地复杂。自定义小部件类应该像任何其他小部件类一样对待。无需像这样将其传递到 MainApplication 构造函数中 - 只需直接在 initUi 内创建它即可:

def initUI(self):
    ...        
    self.widget = MemoryGame()
    self.setCentralWidget(self.widget)

关于python - PyQt5:通过小部件方法在 QAction 之后更新 MainApplication 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39729424/

相关文章:

python - 功能无法正常工作

python - RoboBrowser 的不同网站响应

Python google cloud function部署失败-Madmom pip包

python - 无法导入 numpy : AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'

"immutable class"的 Java 构造函数具有许多具有默认值的字段?

python - 计算字典的字典中的值的数量

python - 安装 win_unc 库时出现 unicode 错误

python - Pandas :按未知时间段分组

C#:糟糕的类设计(第一个 OOP)

C++释放宽字符的内存