Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python pyqt中从其他类调用函数_Python_Qt_Pyqt5_Designer - Fatal编程技术网

如何在python pyqt中从其他类调用函数

如何在python pyqt中从其他类调用函数,python,qt,pyqt5,designer,Python,Qt,Pyqt5,Designer,请原谅我把事情复杂化了,事情就是这样发生的。我有两个类,一个是windowm,另一个是modelm,每当调用newGame()时,我都试图让它重新启动,下面是一些代码片段: class windowm(QMainWindow): def __init__(self): super(windowm, self).__init__() # a generic widget for the center of the window widget

请原谅我把事情复杂化了,事情就是这样发生的。我有两个类,一个是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()

我不确定我是否理解您的解释,但是
def newGame(self):windowm()
看起来就像您所需要的一切。另外,请尊重Pyhon的惯例,并将您的类名大写。是的,很抱歉有点匆忙,所以它说wondowm没有定义。它还需要其他东西吗?比如我需要添加一些东西吗?或者可能有一件事,比如从导入等。令人钦佩的是,我是python新手,所以你知道我如何在我的实例中做到这一点吗?我被你的意思弄糊涂了,我不知道你被困在哪里了?我的示例代码是否返回了一些错误?它不知道“windowm”是什么错误:self.dothis=windowm(QMainWindow)name错误:未定义名称“windowm”