如何每秒将 QLabel 从 PyQt 更新为新字符串?
How to Update QLabel from PyQt to new string each second?
所以我试图在 python 中编写 运行 文本,但我一直在思考如何每秒更新 Qlabel 中的字符串,使其看起来像 运行文本。这是我的代码
label = QLabel('Hello World ')
label.move(70, 40)
label.setParent(form)
直到现在我只知道如何将字符串的最后一个字符放在第一个字符,它是这样的:
old = label.text()
new = old[len(old)-1:]+old[0:len(old)-1]
label.settext(new)
我需要知道的是如何每秒一遍又一遍地重复我的代码,使文本看起来像 运行。
请教我怎么做,感谢您的赞赏和抽出时间。抱歉我的英语不好。
单击此处查看所有代码。
您必须使用 QTimer
。
def onTimeout():
old = label.text()
new = old[len(old)-1:]+old[0:len(old)-1]
label.setText(new)
label = QLabel("Hello World ")
timer = QTimer()
timer.timeout.connect(onTimeout)
timer.start(1000)
注意:将label.settext()
改为label.setText()
所以我试图在 python 中编写 运行 文本,但我一直在思考如何每秒更新 Qlabel 中的字符串,使其看起来像 运行文本。这是我的代码
label = QLabel('Hello World ')
label.move(70, 40)
label.setParent(form)
直到现在我只知道如何将字符串的最后一个字符放在第一个字符,它是这样的:
old = label.text()
new = old[len(old)-1:]+old[0:len(old)-1]
label.settext(new)
我需要知道的是如何每秒一遍又一遍地重复我的代码,使文本看起来像 运行。
请教我怎么做,感谢您的赞赏和抽出时间。抱歉我的英语不好。
单击此处查看所有代码。
您必须使用 QTimer
。
def onTimeout():
old = label.text()
new = old[len(old)-1:]+old[0:len(old)-1]
label.setText(new)
label = QLabel("Hello World ")
timer = QTimer()
timer.timeout.connect(onTimeout)
timer.start(1000)
注意:将label.settext()
改为label.setText()