python - 我将如何在 PyQt5 中销毁和重新创建我的窗口?
问题描述
我正在练习多文件编程,并正在实现一个简单的扫雷游戏。当用户开始一个新游戏时,我在我的窗口主体中创建了一个 QPushButton 网格。
问题是每当玩家想要开始新的扫雷游戏时,我不知道如何调整窗口大小并重置我的 QPushButtons 网格,以便可以玩新的扫雷游戏。我最初的方法是核对原始窗口并使用以下内容创建一个全新的窗口
def newEasyGame(self):
app = QApplication([])
window = minesweeperWindow(10, 10, "easy")
window.show()
app.exec()
我收到错误QCoreApplication::exec: 事件循环已在 运行。
编辑:根据要求,这是一个更完整的代码块,更详细地描述了我想要做的事情
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class minesweeperDemoWindow(QMainWindow):
def __init__(self, rows, cols, difficulty):
super(minesweeperDemoWindow, self).__init__()
#Central widget that is everything is contained within
widget = QWidget()
self.setCentralWidget(widget)
#Vertical layout with a grid of buttons inside of it
layout = QVBoxLayout()
widget.setLayout(layout)
#create a grid of QPushButtons that act as the playing field
self.playingField = [[0 for x in range(rows)] for y in range(cols)]
grid = QGridLayout()
#for every row and column, add a button
for r in range(0, rows):
for c in range(0, cols):
button = QPushButton()
button.setFixedSize(30, 30)
#keep track of each button's position with myRow and myCol
button.setProperty("myRow", r)
button.setProperty("myCol", c)
button.clicked.connect(self.buttonClicked)
self.playingField[r][c] = button
grid.addWidget(self.playingField[r][c], r, c)
layout.addLayout(grid)
grid.setSpacing(0)
#How a user is going to start a new game
menu = self.menuBar().addMenu("&Start new Game")
newEasy = QAction("Easy", self, shortcut=QKeySequence.New, triggered=self.newEasyGame)
#when the Easy menu item is selected, destroy the window and create a new easy game
def newEasyGame(self):
app = QApplication([])
window = minesweeperWindow(10, 10, "easy")
window.show()
app.exec_()
解决方案
您不必破坏窗口。在这种情况下,一个可能的解决方案是消除包含 QGridLayout 的小部件。为此,必须创建一个方法,在必要时实现销毁逻辑并创建按钮。
from PyQt5 import QtCore, QtGui, QtWidgets
class MinesweeperDemoWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MinesweeperDemoWindow, self).__init__(parent)
levels = [
(4, 4, "level1"),
(8, 8, "level2"),
(16, 16, "level3"),
(32, 32, "level4"),
]
menu = self.menuBar().addMenu("&Start new Game")
for r, c, name in levels:
action = menu.addAction(name)
action.setData((r, c))
action.triggered.connect(self.on_triggered)
r, c, _ = levels[0]
self.setSize(r, c)
@QtCore.pyqtSlot()
def on_triggered(self):
action = self.sender()
r, c = action.data()
self.setSize(r, c)
def setSize(self, rows, cols):
# delete the old container
widget = self.centralWidget()
if widget is not None:
widget.deleteLater()
# create new container
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
grid = QtWidgets.QGridLayout(widget)
self.playingField = [[0 for x in range(rows)] for y in range(cols)]
for r in range(rows):
for c in range(cols):
button = QtWidgets.QPushButton()
button.setFixedSize(30, 30)
# keep track of each button's position with myRow and myCol
button.setProperty("myRow", r)
button.setProperty("myCol", c)
# button.clicked.connect(self.buttonClicked)
self.playingField[r][c] = button
grid.addWidget(button, r, c)
self.setFixedSize(widget.sizeHint())
@QtCore.pyqtSlot()
def buttonClicked(self):
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MinesweeperDemoWindow()
w.setSize(4, 4)
w.show()
sys.exit(app.exec_())
推荐阅读
- jqgrid - 如何在 jqgrid 中更改行位置?
- firebase - 如何在 Firebase 和 Visual Studio 之间连接?
- ios - 在 Swift 中合并两个结构数组
- python - 使用 python 查找昨天在 linux 主机上创建的所有“*.wav”文件
- c++ - 无法让 cmake 链接到 GLFW 库
- java - Processbuilder 抛出 IO 异常
- linux - 使用 linux 命令在匹配行的末尾添加带有目录结构的字符串
- docker - 如何在不在 docker.com 上创建帐户并登录的情况下下载 Docker for Windows?
- generics - 在 Kotlin 列表中使用泛型时出现类型不匹配错误
- python - django 在生产环境中,urls.py 仅匹配空路径,即“”,仅此而已?