Обновление метки с использованием таймера в PyQt - PullRequest
0 голосов
/ 08 февраля 2019

Я пытаюсь обновлять QLabel каждые 0,2 секунды, используя список слов.Вот мой текущий код:

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)

Это показывает метку и только одно слово и не обновляется.Я новичок, поэтому любые советы будут фантастическими.Спасибо

1 Ответ

0 голосов
/ 08 февраля 2019

Попробуйте:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...