PyQt 中秒表的固定增量

问题描述 投票:0回答:0

我设计了一个具有启动、重置、停止功能的多秒表程序。 每个秒表都可以独立运行,但我遇到了一个问题,即计时器在一秒钟内递增两次。我不确定是什么原因造成的以及如何调试它。下面是我一直在使用的代码。一旦功能正确,我希望将来使用 GPIO 按钮而不是屏幕上的按钮。

class TimerApp(QMainWindow):
    def __init__(self):
        super().__init__()
 
        # Load the UI file
        self.load_ui()
 
        # Create timers
        self.timer1 = QTimer()
        self.timer1.timeout.connect(self.update_timer1)
        self.elapsed_time1 = 0
        self.is_timer1_running = False
 
        self.timer2 = QTimer()
        self.timer2.timeout.connect(self.update_timer2)
        self.elapsed_time2 = 0
        self.is_timer2_running = False
 
        # Connect start and reset buttons to their respective slots
        self.start_button1.clicked.connect(self.start_timer1)
        self.start_button2.clicked.connect(self.start_timer2)
        self.reset_button1.clicked.connect(self.reset_timer1)
        self.reset_button2.clicked.connect(self.reset_timer2)
 
        # Create and connect stop button
        self.stop_button = QPushButton(self.centralwidget)
        self.stop_button.setGeometry(360, 290, 75, 23)
        self.stop_button.setText("STOP ALL")
        self.stop_button.clicked.connect(self.stop_timers)
 
        # Initialize timer labels
        self.timer1_display.setText("00:00:00")
        self.timer2_display.setText("00:00:00")
 
        # Create and start the update timer
        self.update_timer = QTimer()
        self.update_timer.timeout.connect(self.update_timers)
        self.update_timer.start(1000)
 
    def load_ui(self):
        # Load the UI file
        from PyQt5.uic import loadUi
        loadUi("timer_app.ui", self)
 
        # Get references to timer labels
        self.timer1_display = self.timer1_label
        self.timer2_display = self.timer2_label
 
    def start_timer1(self):
        if not self.timer1.isActive():
            self.timer1.start(1000)
        else:
            self.timer1.start()
 
    def start_timer2(self):
        if not self.timer2.isActive():
            self.timer2.start(1000)
        else:
            self.timer2.start()
 
 
    def reset_timer1(self):
        self.timer1.stop()
        self.elapsed_time1 = 0
        self.timer1_display.setText("00:00:00")
 
    def reset_timer2(self):
        self.timer2.stop()
        self.elapsed_time2 = 0
        self.timer2_display.setText("00:00:00")
 
    def stop_timers(self):
        self.timer1.stop()
        self.timer2.stop()
 
 
    def update_timers(self):
        print("Updating timers")
        try:
            if self.timer1.isActive():
                self.update_timer1()
            if self.timer2.isActive():
                self.update_timer2()
        except Exception as e:
            print("Error occurred:", str(e))
 
 
    def update_timer1(self):
        self.elapsed_time1 += 1
        self.timer1_label.setText(self.format_time(self.elapsed_time1))
 
    def update_timer2(self):
        self.elapsed_time2 += 1
        self.timer2_label.setText(self.format_time(self.elapsed_time2))
 
    @staticmethod
    def format_time(milliseconds):
        seconds = milliseconds % 60
        minutes = (milliseconds // 60) % 60
        hours = milliseconds // 3600
 
        time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
        return time_str
python pyqt
© www.soinside.com 2019 - 2024. All rights reserved.