如何在python-pyqt中从不同的类调用函数



请原谅我把这件事搞得过于复杂,事情就是这样发生的。所以我有这两个类,一个是windowm,另一个是modelm,每当调用newGame((时,我都试图让它重新启动,所以这里有一些代码片段:

class windowm(QMainWindow):
def __init__(self):
super(windowm, self).__init__()
# a generic widget for the center of the window
widget = QWidget()
self.setCentralWidget(widget)

另一类:

class Model:
def __init__(self):
# setup is just starting a new game, so use that method
self.newGame()
def newGame(self):
super(windowm, self).__init__()

是的,我知道这很复杂,原谅我,这就是任务的方式。所以我知道,在我遇到这个令人讨厌的独特场景之前,这个问题已经得到了回答。因此,正如您在第二个代码片段中看到的那样,我正试图让它跳回到类"windowM"中,并跳到函数init(self(中,以重新启动游戏。请帮忙,谢谢!

您必须创建另一个类的新实例,然后使用它来调用游戏函数。

我想你会想改变你的游戏类,这样从另一个类开始新游戏会更容易。

class Model:
def startNewGame(self):
# setup is just starting a new game, so use that method
self.newGame()
def newGame(self):
super(windowm, self).__init__()

然后它可以这样使用:

class windowm(QMainWindow):
def __init__(self):
super(windowm, self).__init__()
# a generic widget for the center of the window
widget = QWidget()
self.setCentralWidget(widget)
# create an instance of the other class
ng = Model()
# start a new game
ng.startNewGame()

最新更新