2016-11-17 3 views
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' 
+1

@CroMagnon. 편집 할 때 서명 줄, "감사"등을 제거하십시오. 이는 노이즈로 간주됩니다. [ "안녕, 고마워, 태그 라인 및 인사말을 게시물에서 삭제해야합니까"] (http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from- 게시물). –

+0

timer.timeout.connect는 슬롯 (파이썬에서 호출 가능)에 연결해야하며, Label은 setText에서 반환하는 것과 비슷하지 않을 수 있습니다. 이것은 호출 가능하지 않습니다. Qt의 신호와 슬롯에 대해서 더 읽고, 일반적으로 파이썬에 대해서도 읽어보십시오. – Trilarion

답변

0

선생님, 코드가 엉망 하하하 .. 당신이 방법의 울부 짖는 소리에이 라벨이 없기 때문에 발생이 오류는, 그것은 "자기"로 선언되어야한다. 그래서 그것은 세계적이고 당신은 그것에 접근 할 수 있습니다.

정의하는 방식에 이상한 많은 것들이 있습니다.

  1. 파이썬 API에서 보라 :

    몇 가지 조언을.

  2. Qt API
  3. 다운로드 간단한 코드 예제의 모양과 사물이 작동하는 방법에서 찾아 보게한다 : D합니다. 예 : here. 원하는 경우 가져 오기를 PyQt5로 변경하면됩니다.

어쨌든,

하면이 같은 것을보십시오. 나는 약간의 수정을 ... 만든

#!/usr/bin/python3 
# -*- coding: utf-8 -*- 


import sys, time 

from PyQt5.QtCore import QTimer 
from PyQt5.QtWidgets import QWidget, QLabel, QGridLayout, QApplication 



class race(QWidget): 
    lane1 = None 

    timer = None 

    def __init__(self): 
     super().__init__() 
     self.initUI() 


    def initUI(self): 

     self.lane1 = QLabel('READY?') 

     self.timer = QTimer() 

     self.grid = QGridLayout() 
     self.grid.setSpacing(10) 

     self.grid.addWidget(self.lane1, 1, 0, 1, 1) 

     self.setLayout(self.grid) 


     self.setGeometry(400, 300, 600, 300) 
     self.setWindowTitle('Race') 
     self.showMaximized() 

     self.update_label() 

    def update_label(self): 



     self.timer.start(99999999) 
     self.timer.setInterval(999999) 
     self.lane1.setText(str(self.timer.remainingTime())) 

    def paintEvent(self, q_painter_event): 
     self.lane1.setText(str(self.timer.remainingTime())) 
     super(race, self).paintEvent(q_painter_event) 



if __name__ == '__main__': 

    raceApp = QApplication(sys.argv) 
    racePanel = race() 
    sys.exit(raceApp.exec_()) 

당신은 그냥 가야 당신이 원하는에 적응 : P 나는 당신의 편집 승인