PyQt:标签未显示正确的长度数

2024-09-30 16:37:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用PyQt4做测验申请,有3个主要生成器:

IncorrectCorrectTimeout

所有答案都连接到计分功能:

def scorecheck(self, sendercheck):
    if ( sendercheck == self.answ ) or ( sendercheck == self.answ1 ) or ( sendercheck == self.answ5 ) or ( sendercheck == self.answ7 ) or ( sendercheck == self.answ8 ) or ( sendercheck == self.answ10 ):
        self.wronganswers.append(1)
    elif ( sendercheck == self.answ2 ) or ( sendercheck == self.answ4 ) or ( sendercheck == self.answ9 ):
        self.correctanswers.append(1)
    elif sendercheck == self.tmr3:
        self.timeouts.append(1)
        print "Worked"
        print len(self.timeouts)
    else:
        pass


    self.wronganswerlabel = QtGui.QLabel(str(len(self.wronganswers)), self)
    self.wronganswerlabel.setGeometry(220, 40, 200, 200)
    self.wronganswerlabel.setObjectName('wronganswercount')
    self.wronganswerlabel.setStyleSheet("#wronganswercount { font-size: 31pt; color: white; border: none; }")


    self.timeoutlabel = QtGui.QLabel(str(len(self.timeouts)), self)
    self.timeoutlabel.setGeometry(480, 80, 200, 200)
    self.timeoutlabel.setObjectName('timeoutlabel')
    self.timeoutlabel.setStyleSheet("#timeoutlabel { font-size: 31pt; color: white; border: none; }") 


    self.correctanswerlabel = QtGui.QLabel(str(len(self.correctanswers)), self)
    self.correctanswerlabel.setGeometry(1050, 40, 200, 200)
    self.correctanswerlabel.setObjectName('correctanswercount')
    self.correctanswerlabel.setStyleSheet("#correctanswercount { font-size: 31pt; color: white; border: none }")




    summary = len(self.correctanswers) + 100 - len(self.wronganswers) - len(self.timeouts) % 10


    if summary < 0:
        while summary < 0:
            summary += 1



    self.summarylabel = QtGui.QLabel(str(summary), self)
    self.summarylabel.setGeometry(850, 222, 200, 200)
    self.summarylabel.setObjectName('summary')
    self.summarylabel.setStyleSheet("#summary { font-size: 40pt; color : white; border: none }")

当损失计数将达到3,它将激活gameover功能,其中将显示标签

ErrorAnswersLabel和correctanswerslabel正常显示所有内容,但timeoutlabel显示0

但是我在scorecheck和gameover函数的一般部分添加了print len(timeouts),所有结果都等于1

我试过这个: self.timeouts.append(1)self.timeouts.append(1)

奇怪的是标签上显示的是2

我还尝试了多处理记分检查功能,认为这是性能问题,但没有更新

有什么问题吗?为什么timeoutlabel显示0而其他所有的都显示正确的数字?是因为性能问题还是输入错误


Tags: orselflensummarytimeoutsappendstrqtgui
1条回答
网友
1楼 · 发布于 2024-09-30 16:37:44

我已经解决了这个问题:

功能顺序:

二 三 2 三 1 2 三 4 五

2位于定义的标签顶部

3位于已定义标签的底部

1是超时的sendercheck语句((重要)

4位于所示标签的顶部

5显示在标签下

定义计时器的位置:

    self.tmr3 = QtCore.QTimer(self)
    self.tmr3.setSingleShot(True)
    self.tmr3.timeout.connect(partial(self.Timeout, self.tmr3)) # It connects to timeout first, thats why sequence is going wrong.
    self.tmr3.timeout.connect(partial(self.scorecheck, self.tmr3))
    self.tmr3.timeout.connect(partial(self.checkifout, self.tmr3))

通过在第一个超时信号时添加scorecheck来修复此问题:

    self.tmr3 = QtCore.QTimer(self)
    self.tmr3.setSingleShot(True)
    self.tmr3.timeout.connect(partial(self.scorecheck, self.tmr3))
    self.tmr3.timeout.connect(partial(self.Timeout, self.tmr3))
    self.tmr3.timeout.connect(partial(self.checkifout, self.tmr3))

相关问题 更多 >