1
我是新來的蟒蛇,我真的希望得到一些幫助。比賽倒計時部件
我想創建一個自動槽車圈數/定時器的應用程序。 我需要在比賽開始前進行一些倒計時,並且我正在使用QLabel。
我想用'update_label()'函數更新它,但它似乎不工作。
我做錯了什麼?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, time
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QLineEdit, QGridLayout, QApplication, QLCDNumber, QSlider
from PyQt5.QtCore import QCoreApplication, Qt
class race(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lane1 = QLabel('READY?')
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(lane1, 1, 0, 1, 1)
self.setLayout(grid)
self.setGeometry(400, 300, 600, 300)
self.setWindowTitle('Race')
self.showMaximized()
self.update_label()
def update_label(self):
count = 3
while count>1:
time.sleep(1)
counter = count
Label = self.lane1.setText(counter)
timer = QtCore.QTimer()
timer.timeout.connect(Label)
timer.start(10000)
count = count - 1
if __name__ == '__main__':
raceApp = QApplication(sys.argv)
racePanel = race()
sys.exit(raceApp.exec_())
但引發此錯誤:
Traceback (most recent call last):
File "./race.py", line 58, in <module>
racePanel = race()
File "./race.py", line 16, in __init__
self.initUI()
File "./race.py", line 35, in initUI
self.update_label()
File "./race.py", line 45, in update_label
Label = self.lane1.setText(counter)
AttributeError: 'race' object has no attribute 'lane1'
@CroMagnon我批准你的編輯。在編輯時,請刪除簽名線,「謝謝」等。這些被認爲是噪音。見[「應該你好,謝謝,標語和稱呼從帖子中刪除」](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-帖子)以獲取更多信息。 –
timer.timeout.connect應該連接到一個插槽(在python中可調用),Label是無論setText返回的,可能都是void。這不是一個可調用的。請閱讀關於Qt中的信號和插槽的更多信息,並且可能還會涉及到Python的一般信息。 – Trilarion