【发布时间】:2019-09-10 21:00:27
【问题描述】:
我正在练习多文件编程并正在实现一个简单的扫雷游戏。当用户开始一个新游戏时,我在我的窗口主体中创建了一个 QPushButton 网格。
问题是每当玩家想要开始一个新的扫雷游戏时,我不知道如何调整我的窗口大小并重置我的 QPushButtons 网格,以便可以玩一个新的扫雷游戏。我最初的方法是核对原始窗口并使用以下内容创建一个全新的窗口
def newEasyGame(self):
app = QApplication([])
window = minesweeperWindow(10, 10, "easy")
window.show()
app.exec()
我收到错误QCoreApplication::exec: event loop is already running.
编辑:根据要求,这是一个更完整的代码块,更详细地描述了我正在尝试做的事情
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_()
【问题讨论】:
-
@eyllanesc 对任何尚不清楚的地方表示歉意。我的问题是由于无法在游戏之间调整网格大小,所以我的解决方案是直接删除原始窗口并重新创建一个具有正确游戏网格大小的新窗口
-
不要认为您应该询问根本问题,而不是没有人保证可行或正确的可能解决方案,这就是所谓的XY problem,请编辑您的问题,重点关注您的网格问题
-
感谢您的反馈,我知道始终提出正确的问题很重要,但您提供了如何做到这一点的实际示例。非常感谢!