PyQt4:如何让第一个小部件覆盖其他小部件? (使其成为其他小部件的父级)

时间:2016-04-29 07:53:47

标签: python pyqt pyqt4 qwidget

注意:如果无法做到这一点,那么是否可以在不更改完整代码的情况下使用QStackedLayout创建页面?怎么样?

我在PyQt4中创建了一个Quiz应用程序,由于这个问题,它很无组织,例如:

答案功能不正确,答案功能正确,超时功能正常,问题1每隔12秒就会调出超时。

我在Question1函数中添加了心(HP):Heart,Heart1,Heart2,这是在Timeout之前,错误和正确的函数。

每当用户按下错误答案时,它将执行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)

我几乎没有显示按钮等的东西(它们都在功能中列出)

现在我决定将这个添加到Timeout功能上,这个功能不顺利,继续按钮不显示,Go back按钮不会显示,也不会显示心脏。

所以这是超级无组织的地方,我添加了Continue1,Goback1,Heart3,Heart4和Heart5。

不仅是Timeout,它甚至不会显示在正确的答案功能上,所以我也有不同的添加。

但是在视觉上不可能修改它们,因为如果在2个心中错误地回答错误,在第二个问题上,如果他们在Timeout上失败,他们将再次拥有2颗心,因为心脏不同定义即可。然而,不可能为此设置布尔值,所以如果heart1被隐藏,那么heart3也是一样,因为它们需要被执行所以程序知道它已被定义。

但最后,我只是隐藏了Timeout图片,(它是1280 x 800,背景图片),我看到了那些心和按钮。

问题:

如何使用 widget2 覆盖 widget1 或使 widget1 widget1 的父级?

因此它可以在widget1上显示widget2。

如果这不太可能,我如何使用QStackedLayout创建页面?

完整代码(不推荐):

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

0 个答案:

没有答案