尝试构建秒表GUI,但我无法使暂停功能正常工作。每当我暂停计时器然后重新开始时,计时器就会以每秒一秒的速度运行。
import sys
import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QGridLayout
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QPalette
class StopWatch(QWidget):
def __init__(self):
super().__init__()
self.isRunning = False
self.counter = 0
self.setWindowTitle('Stop Watch')
self.create_layout()
def create_layout(self):
self.timer = QTimer()
self.timer.setInterval(1000)
labelFormating = QPalette()
labelFormating.setColor(QPalette.WindowText, Qt.darkBlue)
self.startButton = QPushButton('Start')
self.pauseButton = QPushButton('Pause')
self.stopButton = QPushButton('Stop')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.startButton.clicked.connect(self.start_timer)
self.pauseButton.clicked.connect(self.pause)
self.stopButton.clicked.connect(self.stop_timer)
self.displayLabel = QLabel(str(datetime.timedelta(seconds=0)))
self.displayLabel.setFont(QFont('Open Sans', 24))
self.displayLabel.setPalette(labelFormating)
gridLayout = QGridLayout()
gridLayout.addWidget(self.displayLabel, 0, 0, 1, 3) # row span | color span (extend)
gridLayout.addWidget(self.startButton, 1, 0, 1, 1)
gridLayout.addWidget(self.pauseButton, 1, 1, 1, 1)
gridLayout.addWidget(self.stopButton, 1, 2, 1, 1)
gridLayout.setAlignment(self.displayLabel, Qt.AlignCenter)
self.setLayout(gridLayout)
def start_timer(self, slot):
print('start clicked')
self.startButton.setEnabled(False)
self.pauseButton.setEnabled(True)
self.stopButton.setEnabled(True)
self.timer.start()
self.timer.timeout.connect(self.run) #This signal is emitted when the timer times out.
def stop_timer(self):
print('Stop clicked')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.isRunning = False
self.timer.stop()
self.displayLabel.setText(str(datetime.timedelta(seconds=0)))
def run(self):
# print('Counter value', str(self.counter))
self.counter += 1
self.display()
def pause(self):
print('pause clicked')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(True)
self.timer.stop()
self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def display(self):
# print(str(datetime.timedelta(seconds=self.counter)))
self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def main():
app = QApplication(sys.argv)
window = StopWatch()
window.show()
sys.exit(app.exec_())
main()
我正在寻找执行暂停功能。例如,当我单击“开始”,然后单击“暂停”,然后单击“开始”时,希望计数器正常运行。任何帮助表示赞赏。
答案 0 :(得分:1)
尝试一下:
import sys
import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QGridLayout
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QPalette
class StopWatch(QWidget):
def __init__(self):
super().__init__()
self.isRunning = False
self.counter = 0
self.setWindowTitle('Stop Watch')
self.create_layout()
def create_layout(self):
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.run) # +++
labelFormating = QPalette()
labelFormating.setColor(QPalette.WindowText, Qt.darkBlue)
self.startButton = QPushButton('Start')
self.pauseButton = QPushButton('Pause')
self.stopButton = QPushButton('Stop')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.startButton.clicked.connect(self.start_timer)
self.pauseButton.clicked.connect(self.pause)
self.stopButton.clicked.connect(self.stop_timer)
self.displayLabel = QLabel(str(datetime.timedelta(seconds=0)))
self.displayLabel.setFont(QFont('Open Sans', 24))
self.displayLabel.setPalette(labelFormating)
gridLayout = QGridLayout()
gridLayout.addWidget(self.displayLabel, 0, 0, 1, 3)
gridLayout.addWidget(self.startButton, 1, 0, 1, 1)
gridLayout.addWidget(self.pauseButton, 1, 1, 1, 1)
gridLayout.addWidget(self.stopButton, 1, 2, 1, 1)
gridLayout.setAlignment(self.displayLabel, Qt.AlignCenter)
self.setLayout(gridLayout)
def start_timer(self, slot):
self.startButton.setEnabled(False)
self.pauseButton.setEnabled(True)
self.stopButton.setEnabled(True)
self.timer.start()
# self.timer.timeout.connect(self.run) # ----
def stop_timer(self):
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.isRunning = False
self.timer.stop()
# ? self.displayLabel.setText(str(datetime.timedelta(seconds=0)))
self.counter = 0 # +++
def run(self):
self.counter += 1
self.display()
def pause(self):
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(True)
self.timer.stop()
# self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def display(self):
self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def main():
app = QApplication(sys.argv)
window = StopWatch()
window.show()
sys.exit(app.exec_())
main()