1

私は何日もインターネットを検索しましたが、このコードを機能させる方法を理解することができます。これは、液晶ディスプレイとボタンを備えた非常にシンプルなGUI(Qt Designerで作成)です。ボタンを押すだけで180秒前からカウントダウンを開始したいです。最初の瞬間、私はボタンを1つの値に減らすことができましたが、非常に多くの異なることを試した後、何も機能していません。誰かが私を助けてくれますか?おそらく非常に単純なものです。ありがとうございました。

# -*- coding: utf-8 -*-
import sys
import time
from PyQt4 import QtCore, QtGui
from relogio import Ui_relogiocc

class StartQT4(QtGui.QMainWindow):
 def __init__(self, parent=None):
  QtGui.QWidget.__init__(self, parent)
  self.ui = Ui_relogiocc()
  self.ui.setupUi(self)
  self.timer = QtCore.QTimer()
  text = "%d:%02d" % (180/60,180 % 60)
  self.ui.QLCDNumber.display(text)
  self.timer.start(1000)
  self.ui.iniciar.clicked.connect(self.updateTimerDisplay)


 def updateTimerDisplay(self):
  self.inicio = 180
  while self.inicio != 0:
   text = "%d:%02d" % (self.inicio/60,self.inicio % 60)
   self.ui.QLCDNumber.display(text)
   self.inicio - 1
  else:
   self.timer.stop()  


if __name__ == "__main__":
 app = QtGui.QApplication(sys.argv)
 myapp = StartQT4()
 myapp.show()
 sys.exit(app.exec_())
4

3 に答える 3

1

ここで見逃していることがたくさんあるようです。

timeout()まず、各タイムアウト期間が完了するたびに、タイマーが信号を発信します。あなたの場合、これは1秒ごとです。ただし、この信号は何にも接続していません。

次に、updateTimerDisplayには次の行が含まれています。

   self.inicio - 1

これは、の値を読み取り、self.inicioそこから1を引いて、結果を破棄します。self.inicioの値は変更されないため、メソッドupdateTimerDisplayは無限ループに入ります。

私はあなたが意味したと思います

   self.inicio -= 1

self.inicio代わりに、 backの新しい値をそれ自体に割り当てます。

ただし、最終的には、updateTimerDisplayメソッドを使用してタイマーを開始し、カウントダウンし、タイマーの表示を更新しようとしているようです。このメソッドをより小さなメソッドに分割することをお勧めします。

まず、updateTimerDisplayタイマーの表示のみを更新する必要があります。

 def updateTimerDisplay(self):
  text = "%d:%02d" % (self.inicio/60,self.inicio % 60)
  self.ui.QLCDNumber.display(text)

次に、タイマーを開始するメソッドが必要になります。次のようなことをする必要があります:

 def startTimer(self):
  self.inicio = 180
  self.updateTimerDisplay()
  self.timer.start(1000)

もちろん、iniciarボタンのclicked()信号をではなく、この関数に接続する必要もありますupdateTimerDisplay

最後に、タイマーからのティックを処理するメソッドが必要になります。次のようなことをする必要があります:

 def timerTick(self):
  self.inicio -= 1
  self.updateTimerDisplay()
  if self.inicio <= 0:
   self.timer.stop()

timeout()また、次のようなものを使用して、タイマーの信号をこの関数に接続する必要があります。

  self.timer.timeout.connect(self.timerTick)
于 2012-09-30T13:23:09.710 に答える
1

他の回答で言われているように、あなたのコードにはいくつかの明らかな間違いが含まれています。ここに、ボタンがクリックされるたびにカウンターを適切にリセットする(つまり、タイマーを停止してから再開する)完全な実例があります(UIはDesignerを介して作成されません)。そうしない場合は、Startボタンをクリックしてからタイマーが停止すると、ボタンがクリックされるたびにカウンターのカウントが速くなります)。

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.central = QWidget(self)
        self.hbox = QHBoxLayout(self.central)
        self.lcd = QLCDNumber(self.central)
        self.timer = QTimer(self)
        self.start_time = 20
        self.lcd.display("%d:%02d" % (self.start_time/60,self.start_time % 60))
        self.start_button = QPushButton("Start", self.central)
        self.hbox.addWidget(self.lcd)
        self.hbox.addWidget(self.start_button)
        self.setCentralWidget(self.central)

        self.start_button.clicked.connect(self.restartTimer)
        self.timer.timeout.connect(self.updateLCD)

    def restartTimer(self):
        # Reset the timer and the lcd
        self.timer.stop()
        self.start_time = 20
        self.lcd.display("%d:%02d" % (self.start_time/60,self.start_time % 60))
        # Restart the timer
        self.timer.start(1000)

    def updateLCD(self):
        # Update the lcd
        self.start_time -= 1
        if self.start_time >= 0:
            self.lcd.display("%d:%02d" % (self.start_time/60,self.start_time % 60))
        else:
            self.timer.stop()

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    ui = MyMainWindow()
    ui.show()
    sys.exit(app.exec_())
于 2012-09-30T14:10:42.657 に答える
0

コードには複数の問題があります。手始めに、あなたはself.inicio - 1の代わりに書いた、そしてあなたはあなたが作成し-= 1たものを実際に使うことは決してない。Qtimerしかし、それを無視すると、プログラムの構造は正しくありません。現在updateTimerDisplay、ユーザーがiniciarボタンをクリックしたときに呼び出し、カウントダウンがゼロになるまでそこでループします。ユーザーがボタンをクリックしたときにタイマーを開始し、タイマー(実際にはそのtimeout信号)を1秒カウントダウンして表示を更新するメソッドに接続します。

def startTimerDisplay(self):
    """ set the countdown value and start the timer """
    self.inicio = 180
    self.timer.start(1000)

def updateTimerDisplay(self):
    """ count down one second, set the text, and check if the timer should stop """
    self.inicio -= 1
    text = "%d:%02d" % (self.inicio/60,self.inicio % 60)
    self.ui.QLCDNumber.display(text)
    if self.inicio == 0:
        self.timer.stop()

__init__次のようにこれらの関数を接続するようにメソッドを変更します。

def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_relogiocc()
    self.ui.setupUi(self)
    self.timer = QtCore.QTimer()
    text = "%d:%02d" % (180/60,180 % 60)
    self.ui.QLCDNumber.display(text)
    #connect the button to the start method ...
    self.ui.iniciar.clicked.connect(self.startTimerDisplay)
    #... and the timer to the update method
    self.timer.timeout.connect(self.updateTimerDisplay)
于 2012-09-30T13:22:42.030 に答える