更新使用PyQt的计时器的标签

问题描述 投票:0回答:1

我试图更新使用的单词列表每隔0.2秒QLabel。这里是我当前的代码:

wordLabel = QLabel(window)
wordLabel.setFont(font)
wordLabel.setGeometry(120, 130, 200, 200)
wordLabel.hide()

def onTimeout():
    old = wordLabel.text()
    man = ["uncle", "hammer", "suit", "cigar", "beer", "boy",   "grandpa", "his", "motor", "guy", "razor", "mister","father", "blue", "football", "he", "brother", "tie", "tough", "man"]
    counter=0
    for item in man:
        counter=+1
        wordLabel.setText(str(man[counter]))
timer = QTimer()
timer.timeout.connect(onTimeout)
timer.start(2)

这显示了标签,只有一个字,不更新。我是初学者所以任何提示将是非常美妙。谢谢

python user-interface pyqt
1个回答
0
投票

试试吧:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class MyWin(QWidget):
    def __init__(self, man):
        super().__init__()

        self.man     = man
        self.counter = 0
        self.len_man = len(self.man)

        self.wordLabel = QLabel(self)
        self.wordLabel.setStyleSheet('font-size: 18pt; color: blue;')
        self.wordLabel.setGeometry(20, 20, 100, 50)

        timer = QTimer(self)
        timer.timeout.connect(self.onTimeout)
        timer.start(2000)

    def onTimeout(self):
        if self.counter >= self.len_man:
            self.counter = 0

        self.wordLabel.setText(str(self.man[self.counter]))
        self.counter += 1


man =  ["Hello", "Evans"] #["uncle", "hammer", "suit", "cigar", "beer", "boy",   "grandpa", "his", "motor", "guy", "razor", "mister","father", "blue", "football", "he", "brother", "tie", "tough", "man"]

if __name__ =="__main__":
    app = QApplication(sys.argv)
    app.setFont(QFontDatabase().font("Monospace", "Regular", 14))
    w = MyWin(man)
    w.show()
    sys.exit(app.exec())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.