Q: PyQt4: How to make first widget overwrite other widget? (making it parent of other widget) Note: If this is not possible, then is it possible to make pages with QStackedLayout without changing full code? and how?
I have made a Quiz application in PyQt4, which got pretty unorganized because of this problem, For example:
There was incorrect answer function, correct answer function and timeout function, Timeout was getting called after every 12 seconds on question1.
I added hearts (HP): Heart, Heart1, Heart2 on Question1 function, which was before Timeout, incorrect and Correct functions.
Whenever user pressed on incorrect answer, it would execute boolean:
if self.heart2.isHidden():
    self.heart1.hide()
    heartlosscount.append(1)
if self.heart1.isHidden():
    self.heart.hide()
    heartlosscount.append(2)
else:
    self.heart2.hide()
    heartlosscount.append(3)

I had few things showing buttons, etc. (They were all listed in function)
Now i decided to add this on Timeout function, which didn't go out well, Continue button wouldn't show, Go back button wouldn't show and neither would hearts.
So this is where it got super unorganized, i added Continue1, Goback1, Heart3, Heart4 and Heart5.
It wasn't only Timeout, it even wouldn't show on Correct answer function, so i added there differently too.
But then it was visually impossible to modify them, Because if had failed an answer on incorrect with having 2 hearts, on second question, if they failed on Timeout, they would have 2 hearts again, because hearts were differently defined. However it was not possible to make boolean for that, so if heart1 was hidden it would be same for heart3, because they needed to be executed so program knew it was defined.
But at the end, i just set Timeout picture hidden, (It was 1280 x 800, background image) and i saw those hearts and buttons.

Question:
How is it possible to overwrite widget1 with widget2 or making widget2 parent of widget1?
So it can show widget2 over widget1.
If this is not quite possible, how can i make pages with QStackedLayout?

Full Code (Not recommended):
import sys
from PyQt4 import QtGui, QtCore
import resources
from functools import partial


class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(0, 0, 1280, 800)
        self.setWindowTitle("E.S Quiz")
        #self.setWindowIcon(QtGui.QIcon(':/newPrefix/download.jpeg'))
        self.home()

     
    def home(self):
        self.pic = QtGui.QLabel(self)
        self.pic.setGeometry(0, 0, 1280, 800)
        self.pic.setPixmap(QtGui.QPixmap(":/images/Background.png"))
        self.btn = QtGui.QPushButton("", self)
        self.btn.resize(150, 120)
        self.btn.move(550, 400)
        self.btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.btn.setObjectName('btn')
        self.btn.setStyleSheet("#btn {background-image: url(':/images/Button1.png'); border: none; }"
        "#btn:hover { background-image: url(':/images/Button1Hover.png'); }"
        "#btn:pressed { background-image: url(':/images/Button1Press.png'); }")
        self.btn.clicked.connect(self.page1)
        self.sun = QtGui.QLabel("", self)
        movie = QtGui.QMovie(":/images/Sun.gif")
        self.sun.setMovie(movie)
        movie.start()
        self.sun.move(10, -100)
        self.sun.resize(300, 300)
        self.show()
        self.raise_()
        





    def page1(self):
        self.pic.hide()
        self.btn.hide()
        self.sun.hide()
        self.pic2 = QtGui.QLabel(self)
        self.pic2.setGeometry(0, 0, 1280, 800)
        self.pic2.setPixmap(QtGui.QPixmap(":/images/Menu1.png"))
        self.char1 = QtGui.QPushButton(self)
        self.char1.setObjectName('Char1')
        self.char1.setStyleSheet("#Char1 {background-image: url(':/images/Char1'); border: none; }"
        "#Char1:hover { background-image: url(':/images/Char1_1.png'); }") 
        self.char1.setGeometry(150, 200, 265, 240)    
        self.char2 = QtGui.QPushButton(self)
        self.char2.setObjectName('Char2')
        self.char2.setStyleSheet("#Char2 {background-image: url(':/images/Char2.png'); border: none; }"
        "#Char2:hover { background-image: url(':/images/Char2_2.png'); }")    
        self.char2.setGeometry(500, 200, 265, 240)  
        self.char3 = QtGui.QPushButton(self)
        self.char3.setObjectName('Char3')
        self.char3.setStyleSheet("#Char3 {background-image: url(':/images/Char3.png'); border: none; }"
        "#Char3:hover {background-image: url(':/images/Char3_3.png'); }")   
        self.char3.setGeometry(850, 200, 265, 240) 
        self.unlimited = QtGui.QPushButton(self)
        self.unlimited.setObjectName('Unlimited')              
        self.unlimited.setStyleSheet("#Unlimited {background-image: url(':/images/Unlimited.png'); border: none; }"
        "#Unlimited:hover { background-image: url(':/images/Unlimited_1.png'); }"
        "#Unlimited:pressed { background-image: url(':/images/Unlimited_2.png'); }")
        self.unlimited.setGeometry(480, 500, 300, 100)
        self.char1.clicked.connect(self.question1) 
        self.pic4 = QtGui.QLabel(self)
        self.pic4.setPixmap(QtGui.QPixmap(":/images/Incorrect.png"))
        self.goback = QtGui.QPushButton(self)
        self.goback.setObjectName('goback')
        self.goback.setStyleSheet("#goback {background-image: url(':/images/Goback.png'); border: none; }"
        "#goback:hover { background-image: url(':/images/Goback1.png'); }"
        "#goback:pressed { background-image: url(':/images/Goback2.png'); }")
        self.goback.show()
        self.goback.setGeometry(0, 610, 300, 100)
        self.goback.show()     
        self.goback.clicked.connect(self.gobackfunc)                                                                                           
        self.pic2.show()
        self.char1.show()
        self.char2.show()
        self.char3.show()
        self.unlimited.show()

    def question1(self):
        self.pic2.hide()
        self.char1.hide()
        self.char2.hide()
        self.char3.hide()
        self.unlimited.hide()
        self.pic3 = QtGui.QLabel(self)
        self.pic3.setPixmap(QtGui.QPixmap(":/images/Night.png"))
        self.pic3.setGeometry(0, 0, 1280, 800)
        self.que1 = QtGui.QLabel(self)
        self.que1.setPixmap(QtGui.QPixmap(":/images/Question1.png"))
        self.que1.setGeometry(50, 70, 800, 300)
        self.answ = QtGui.QPushButton(self)
        self.answ.setObjectName('answ')
        self.setStyleSheet("#answ {background-image: url(':/images/Answ1.png'); border: none; }"
        "#answ:hover { background-image: url(':/images/Answ1_1.png'); }")
        self.answ.setGeometry(0, 350, 500, 400)
        self.answ1 = QtGui.QPushButton(self)
        self.answ1.setObjectName('answ1')
        self.answ1.setStyleSheet("#answ1 {background-image: url(':/images/Answ2.png'); border: none; }"
        "#answ1:hover { background-image: url(':/images/Answ2_2.png'); }")
        self.answ1.setGeometry(400, 350, 500, 400)
        self.answ1.clicked.connect(partial(self.incorrect, self.answ1))
        self.answ1.clicked.connect(partial(self.scorecheck, self.answ1))
        self.answ1.clicked.connect(partial(self.checkifout, self.answ1))
        self.answ2 = QtGui.QPushButton(self)
        self.answ2.setObjectName('answ2')
        self.answ2.setStyleSheet("#answ2 {background-image: url(':/images/Answ3.png'); border: none; }"
        "#answ2:hover { background-image: url(':/images/Answ3_3.png'); }")
        self.answ2.setGeometry(800, 350, 500, 400)
        self.answ.clicked.connect(partial(self.incorrect, self.answ))
        self.answ.clicked.connect(partial(self.scorecheck, self.answ))
        self.answ.clicked.connect(partial(self.checkifout, self.answ))
        self.answ2.clicked.connect(partial(self.Correct, self.answ2))
        self.answ2.clicked.connect(partial(self.scorecheck, self.answ2))
        self.pic5 = QtGui.QLabel(self)
        self.pic5.setPixmap(QtGui.QPixmap(":/images/Timeout.png"))
        self.pic5.setGeometry(0, 0, 1280, 800)
        self.pic6 = QtGui.QLabel(self)
        self.pic6.setPixmap(QtGui.QPixmap(":/images/Correct.png"))
        self.pic6.setGeometry(0, 0, 1280, 800)
        self.heart = QtGui.QLabel(self)
        hearts = QtGui.QMovie(":/images/Heart.gif")
        self.heart.setMovie(hearts)
        hearts.start()
        self.heart.move(20, 0)
        self.heart.resize(50, 50)
        self.heart1 = QtGui.QLabel(self)
        hearts1 = QtGui.QMovie(":/images/Heart.gif")
        self.heart1.setMovie(hearts1)
        hearts1.start()
        self.heart1.move(80, 0)
        self.heart1.resize(50, 50)
        self.heart2 = QtGui.QLabel(self)
        hearts2 = QtGui.QMovie(":/images/Heart.gif")
        self.heart2.setMovie(hearts2)
        hearts2.start()
        self.heart2.move(140, 0)
        self.heart2.resize(50, 50)
        self.tmr = QtCore.QTimer(self)
        self.tmr.setSingleShot(True)
        self.tmr.timeout.connect(partial(self.Timeout, self.tmr))
        self.tmr.start(12000)
        self.timer = QtGui.QLabel(self)
        timer1 = QtGui.QMovie(":/images/Timer.gif")
        self.timer.setMovie(timer1)
        timer1.start()
        self.timer.setGeometry(850, 0, 350, 250)
        self.timer.show()
        self.pic3.show()
        self.que1.show()
        self.answ.show()
        self.answ1.show()
        self.answ2.show()
        self.heart.show()
        self.heart1.show()
        self.heart2.show()


 

    def incorrect(self, sendercheck):
        self.timer.hide()
        self.tmr.stop()
        self.pic3.hide()
        self.que1.hide()
        self.answ.hide()
        self.answ1.hide()
        self.answ2.hide()
        self.pic4.setGeometry(0, 0, 1280, 800)
        self.pic4.show()
        self.cont = QtGui.QPushButton(self)
        self.cont.setObjectName('cont')
        self.cont.setStyleSheet("#cont {background-image: url(':/images/Continue.png'); border: none; }"
        "#cont:hover { background-image: url(':/images/Continue1.png'); }"
        "#cont:pressed { background-image: url(':/images/Continue2.png'); }")
        self.cont.setGeometry(980, 610, 300, 300)
        self.cont.clicked.connect(self.incorrecthandler)
        self.goback.show() 
        if sendercheck == self.answ:
            self.cont.show()
        if sendercheck == self.answ1:
            self.cont.show()
        else:
            pass
       

        if self.heart2.isHidden():
            self.heart1.hide()
            heartlosscount.append(1)
        if self.heart1.isHidden():
            self.heart.hide()
            heartlosscount.append(2)
        else:
            self.heart2.hide()
            heartlosscount.append(3)

       



    def Timeout(self, sendercheck):
        self.heart.show()
        self.cont1 = QtGui.QPushButton(self)
        self.cont1.setObjectName('cont1')
        self.cont1.setStyleSheet("#cont1 {background-image: url(':/images/Continue.png'); border: none; }"
        "#cont1:hover { background-image: url(':/images/Continue1.png'); }"
        "#cont1:pressed { background-image: url(':/images/Continue2.png'); }")
        self.cont1.setGeometry(980, 610, 300, 300)
        self.cont1.clicked.connect(self.timeouthandler)
        self.goback2 = QtGui.QPushButton(self)
        self.goback2.setObjectName('goback2')
        self.goback2.setStyleSheet("#goback2 {background-image: url(':/images/Goback.png'); border: none; }"
        "#goback2:hover { background-image: url(':/images/Goback1.png'); }"
        "#goback2:pressed { background-image: url(':/images/Goback2.png'); }")
        self.goback2.clicked.connect(self.gobackfunc)
        self.goback2.setGeometry(0, 610, 300, 100)
        self.pic3.hide()
        self.que1.hide()
        self.answ.hide()
        self.answ1.hide()
        self.answ2.hide()
        self.tmr.stop()
        #self.pic5.show()
        self.goback2.show()
        self.cont1.show() 
        self.goback.setGeometry(200, 200, 400, 400)
    



    def Correct(self, sendercheck):
        self.tmr.stop()
        self.pic6.show()
        self.goback.show()
        self.pic3.hide()
        self.que1.hide()
        self.answ.hide()
        self.answ1.hide()
        self.answ2.hide()
        self.cont2 = QtGui.QPushButton(self)
        self.cont2.setObjectName("Cont2")
        self.cont2.setStyleSheet("#Cont2 {background-image: url(':/images/Continue.png'); border: none; }"
        "#Cont2:hover { background-image: url(':/images/Continue1.png'); }"
        "#Cont2:pressed { background-image: url(':/images/Continue2.png'); }")
        self.cont2.setGeometry(980, 610, 300, 300)
        self.cont2.show()
        self.goback3 = QtGui.QPushButton(self)
        self.goback3.setObjectName('goback3')
        self.goback3.setStyleSheet("#goback3 {background-image: url(':/images/Goback.png'); border: none; }"
        "#goback3:hover { background-image: url(':/images/Goback1.png'); }"
        "#goback3:pressed { background-image: url(':/images/Goback2.png'); }")
        self.goback3.setGeometry(0, 610, 300, 100)
        self.goback3.show()
        self.goback3.clicked.connect(self.gobackfunc)
        self.cont2.clicked.connect(self.correcthandler)





    def checkifout(self, sendercheck):
        heartlosscount = []
        if len(heartlosscount) == 3:
            self.gameover()
        else:
            pass
 
        
           



    def correcthandler(self):
        self.cont2.hide()
        self.goback3.hide()
        self.question2()

   

    def incorrecthandler(self):
        self.cont.hide()
        self.goback.hide()
        self.question2()



    def timeouthandler(self):
        self.cont1.hide()
        self.goback2.hide()
        self.question2()



    def question2(self):
        self.pic4.hide()
        self.pic3.show()
        self.pic6.hide()
        self.pic5.hide()



    def scorecheck(self, sendercheck):
        wronganswers = [] 
        correctanswers = []
        if sendercheck == ( self.answ or self.answ1 ):
            wronganswers.append(1)
            

        if sendercheck == ( self.answ2 ):
            correctanswers.append(1)


        self.wronganswerlabel = QtGui.QLabel("Incorrect: " + str(len(wronganswers)), self)
        self.wronganswerlabel.setGeometry(200, 200, 200, 200)
        
        self.correctanswerlabel = QtGui.QLabel("Correct: " + str(len(correctanswers)), self)
        self.correctanswerlabel.setGeometry(200, 200, 200, 200)

        summary = len(correctanswers) - len(wronganswers)
        self.summarylabel = QtGui.QLabel("Score: " + str(summary), self)
        self.summarylabel.setGeometry(200, 200, 200, 200)





    def gameover(self):
        self.tmr.stop()
        self.Timeout(self)
        self.incorrect(self)
        self.Correct(self)
        self.cont.hide()
        self.cont1.hide()
        self.cont2.hide()
        self.goback3.hide()
        self.goback2.hide()
        self.goback.hide()
        self.pic2.hide()
        self.pic3.hide()
        self.pic4.hide()
        self.pic5.hide()
        self.pic6.hide()
        self.gameoverpic = QtGui.QLabel(self)
        self.gameoverpic.setPixmap(QtGui.QPixmap(":/images/Gameover.png"))
        self.gameoverpic.setGeometry(0, 0, 1280, 800)
        self.gameoverpic.show()
        self.name = QtGui.QLineEdit(self)
        self.name.setGeometry(610, 600, 110, 50)
        self.name.setObjectName('name')
        self.name.setStyleSheet("#name { font-size: 30pt; background-color : cyan; color : yellow; border: none; }")
        self.name.setMaxLength(8)
        self.submit = QtGui.QPushButton(self)
        self.submit.setObjectName('submit')
        self.submit.setStyleSheet("#submit {background-image: url(':/images/Submit.png'); border: none; }"
        "#submit:hover { background-image: url(':/images/Submit_1.png'); }"
        "#submit:pressed { background-image: url(':/images/Submit_3.png'); }")
        self.submit.setGeometry(980, 610, 300, 100)
        self.submit.clicked.connect(self.submitcheck)
        self.name.show()
        self.submit.show()


    
    #def submitcheck(self):
        #if ( len(self.name.text()) < 3 ):

        # This will protect if character count is less than 3 (not done yet)



        

    def gobackfunc(self):
        self.hide()
        self.__init__()



        
def startup():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

startup()

