我正在练习多文件编程,正在实现一个简单的扫雷游戏。当用户开始一个新游戏时,我会在窗口主体中创建一个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的小部件。为此,必须创建一个方法,在必要时实现销毁逻辑并创建按钮。你知道吗
相关问题 更多 >
编程相关推荐