yes itsme
yes itsme

Reputation: 49

how to call a function from a different class in python pyqt

Forgive me for overcomplicating this, thats just how it happened. so i have these two classes one's windowm and the other's modelm, i am trying to get it to re-start whenever newGame() is called so here are some snippets of code:

class windowm(QMainWindow):
    def __init__(self):
        super(windowm, self).__init__()

        # a generic widget for the center of the window
        widget = QWidget()
        self.setCentralWidget(widget)

and the other class:

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__()

yes i understand it is complicated and forgive me for that, that's just how the assignment is. So i understand that this has been answered before i just have this one annoying unique scenario. so as you can see in the second code snippet i am attempting to get it to jump back into the class "windowM" and into the function init(self) to re-start the game. please help and thank you!

Upvotes: 0

Views: 1082

Answers (1)

r0t13
r0t13

Reputation: 39

You'll have to create a new instance of your other class and then use that to call the game function.

I think you'll want to change your game class though, so it's easier to start a new game from the other class.

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__()

Then it can be used like this:

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()

Upvotes: 1

Related Questions