简体   繁体   中英

Game restart PyQT4 Python

I've done simple Hangman game using PyQT4. But i have trouble with creating restart() method.

I tried:

class Ui_MainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(1188, 696)
    MainWindow.setStyleSheet(_fromUtf8("background-color: black;"))
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.statusbar = QtGui.QStatusBar(MainWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    self.buttons = []
    self.setUpKeyboard()
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 1188, 25))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    MainWindow.setMenuBar(self.menubar)
    MainWindow.setStatusBar(self.statusbar)

    self.pic = QtGui.QLabel(self.mainwindow)
    self.pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/img/s0.jpg"))
    self.pic.setGeometry(50, 150, 450, 280)
    self.pic.setObjectName(_fromUtf8("pic"))

....

def restartGame(self):
    self.wordLabel.setText(" ")
    for i in range(26):
        self.buttons[i].setParent(None)
    self.counterLabel.setParent(None)
    self.keyWordHidden = ""
    self.keyWord = ""
    self.pic.setParent(None)
    self.setupUi(self.centralwidget)

In other words im deleting every object created. Somehow i get output:

MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QWidget' object has no attribute 'setCentralWidget'

I'm running out of ideas what i need to pass as argument when i want call my setUpUi() for the second time...

Got another question. My restart works almost fine. But i got another problem. The pixmap that contains graphic element (hanging device building up elements one by one - 10 jpg files like img1, img2...). For the first gameplay it works fine. After 9 mistakes the whole UI should be re-build... But then the pixmap doesn't show up... I tried to remove

self.pic.setParent(None)

and just again switching the "img10.jpg" for "img1.jpg". But this does not help.

The central widget that you created on the main window obviously isn't a main window, it's a widget on a window. So of course trying to use it as MainWindow isn't going to work.

If you want to reuse the same main window, you need to hold onto it so you can reuse it. There may be a better place to do this—it's hard to see how you're using this class without any of the relevant code—but one obvious thing to do is to just store it in setupUI :

def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(1188, 696)
    self.mainwindow = MainWindow
    # …

… and then you can reuse it:

def restartGame(self):
    # …
    self.setupUi(self.mainwindow)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM