如何将字符串追加到全局列表(数组)?

时间:2016-04-29 15:19:25

标签: python arrays python-2.7

我在PyQt4中创建了一个测验应用程序并添加了“心脏”作为健康点,有Heart,Heart1和Heart2。我还添加了Heartlosscount列表,它确定用户是否损失并显示剩余的心脏数量。

    global heartlosscount
    heartlosscount = []


    def somefunction():
        if len(heartlosscount) == 1:
           self.heart2.hide()
           heartlosscount.append(2)
        elif len(heartlosscount) == 2:
           self.heart2.hide()
           self.heart1.hide()
           heartlosscount.append(3)
        elif len(heartlosscount) == 3:
           self.heart2.hide()
           self.heart1.hide()
           self.heart.hide()
        else: 
           self.heart2.hide()
           heartlosscount.append(1)
           print len(heartlosscount)

它在控制台中打印1。 (意味着心脏衰竭计数为1)

每当用户提出第二个问题时,所有人都会出现。作为解决方案,我将其添加到question2函数:

    if len(heartlosscount) == 1:
        self.heart2.hide()
    elif len(heartlosscount) == 2:
        self.heart2.hide()
        self.heart1.hide()
    elif len(heartlosscount) == 3:
        self.heart2.hide()
        self.heart1.hide()
        self.heart.hide()
    else: 
        print len(heartlosscount)

这将在控制台中不打印任何内容(意味着它为空)。

完整代码:

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)
        global heartlosscount
        heartlosscount = []
        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.setObjectName('pic5')
        self.pic5.setStyleSheet("#pic5 { background-image: url(':/images/Timeout.png'); border: none; }")
        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.heartlist()
        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)
        elif self.heart1.isHidden():
            self.heart.hide()
            heartlosscount.append(2)
        else:
            self.heart2.hide()
            heartlosscount.append(3)





    def Timeout(self, sendercheck):
        self.timer.hide()
        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.pic5.show()
        self.tmr.stop()
        self.goback2.show()
        self.cont1.show() 
        self.heartlist()
        self.goback.setGeometry(200, 200, 400, 400)
        self.heart3.show()
        self.heart4.show()
        self.heart5.show()
        if len(heartlosscount) == 1:
            self.heart5.hide()
            self.heart4.hide()
            heartlosscount.append(2)
            print "too"
        elif len(heartlosscount) == 2:
            self.heart5.hide()
            self.heart4.hide()
            self.heart3.hide()
            heartlosscount.append(3)
            print "ones upon"
        else: 
            self.heart5.hide()
            heartlosscount.append(1)
            print "theres no reason"



        self.checkifout(self)



    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)
        self.heart.show()
        self.heart1.show()
        self.heart2.show()
        if len(heartlosscount) == 1:
            self.heart2.hide()
        if len(heartlosscount) == 2:
            self.heart2.hide()
            self.heart1.hide()
        if len(heartlosscount) == 3:
            self.heart2.hide()
            self.heart1.hide()
            self.heart.hide()
        else: 
            print len(heartlosscount)






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



    def heartlist(self):
        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.heart3 = QtGui.QLabel(self)
        hearts3 = QtGui.QMovie(":/images/Heart.gif")
        self.heart.setMovie(hearts3)
        hearts3.start()
        self.heart3.move(20, 0)
        self.heart3.resize(50, 50)
        self.heart4 = QtGui.QLabel(self)
        hearts4 = QtGui.QMovie(":/images/Heart.gif")
        self.heart4.setMovie(hearts4)
        hearts4.start()
        self.heart4.move(80, 0)
        self.heart4.resize(50, 50)
        self.heart5 = QtGui.QLabel(self)
        hearts5 = QtGui.QMovie(":/images/Heart.gif")
        self.heart5.setMovie(hearts5)
        hearts5.start()
        self.heart5.move(140, 0)
        self.heart5.resize(50, 50)





    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()
        if len(heartlosscount) == 1:
            self.heart2.hide()
        elif len(heartlosscount) == 2:
            self.heart2.hide()
            self.heart1.hide()
        elif len(heartlosscount) == 3:
            self.heart2.hide()
            self.heart1.hide()
            self.heart.hide()
        else: 
            print len(heartlosscount)



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

        # Daamate ro ar aris sakmarisi.





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




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

startup()

我认为这是因为我没有将字符串全局追加到数组中。

如果是问题,我该怎么办?

1 个答案:

答案 0 :(得分:1)

您最有可能希望somefunction以这种方式工作:

class Window(QtGui.QMainWindow):

    def home(self):
        self.heartlosscount = []

    def somefunction(self):
        if len(heartlosscount) == 1:
            self.heart2.hide()
            self.heartlosscount.append(2)
        elif len(heartlosscount) == 2:
            self.heart2.hide()
            self.heart1.hide()
            self.heartlosscount.append(3)
        elif len(heartlosscount) == 3:
            self.heart2.hide()
            self.heart1.hide()
            self.heart.hide()
        else: 
            self.heart2.hide()
            self.heartlosscount.append(1)

请注意,您的global定义可以删除。它什么都不做。 除此之外,您的代码还有另一个问题。 当heartlosscount为空或长度为1时,您采取相同的操作 (self.heart2.hide())。我猜你想在这些情况下做不同的事情。