The user interface as shown here
from PyQt5 import QtWidgets, QtGui, QtCore
class CustomWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout()
self.button1 = QtWidgets.QPushButton("Start Timer")
self.button2 = QtWidgets.QPushButton("Stop Timer")
self.label1 = QtWidgets.QLabel("")
self.label1.setWordWrap(True)
self.timer=QtCore.QTimer()
self.layout.addWidget(self.button1, 0, 0)
self.layout.addWidget(self.button2, 0, 1)
self.layout.addWidget(self.label1, 1, 0, 1, 3)
self.setLayout(self.layout)
self.layout.setColumnStretch(2, 1)
self.layout.setRowStretch(2, 1)
self.button1.clicked.connect(self.start_timer)
self.button2.clicked.connect(self.stop_timer)
def start_timer(self):
self.timer.timeout.connect(self.startPressed)
self.timer.start(1000)
self.timer.setInterval(1000)
self.timecount=0
def startPressed(self):
self.label1.setText(str(self.timecount))
self.timecount=self.timecount+1
def stop_timer(self):
self.timer.stop()
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.cw = CustomWidget()
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.cw)
self.setLayout(self.layout)
self.show()
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
app = QtWidgets.QApplication([])
win = App()
status = app.exec_()
I need 1 second interval timer everytime I pressed Start Timer. My problem is first run, the timer will have 1 second interval and then second run, the timer decide to have 2 second interval. The time interval just goes up for 3rd time, 4th time ect. Anyone knows where my silly mistake is?
This is happening because when start_timer(self) is called, you're connecting the self.startPressed method to the timeout signal again without disconnecting the previous connections. This results in multiple connections to the startPressed method.
To fix this, you need to disconnect the previous connection:
from PyQt5 import QtWidgets, QtGui, QtCore
class CustomWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout()
self.button1 = QtWidgets.QPushButton("Start Timer")
self.button2 = QtWidgets.QPushButton("Stop Timer")
self.label1 = QtWidgets.QLabel("")
self.label1.setWordWrap(True)
self.timer = QtCore.QTimer()
self.layout.addWidget(self.button1, 0, 0)
self.layout.addWidget(self.button2, 0, 1)
self.layout.addWidget(self.label1, 1, 0, 1, 3)
self.setLayout(self.layout)
self.layout.setColumnStretch(2, 1)
self.layout.setRowStretch(2, 1)
self.button1.clicked.connect(self.start_timer)
self.button2.clicked.connect(self.stop_timer)
def start_timer(self):
self.timer.timeout.connect(self.startPressed)
self.timer.setInterval(1000) # Set the interval only once
self.timecount = 0
self.timer.start()
def startPressed(self):
self.label1.setText(str(self.timecount))
self.timecount += 1
def stop_timer(self):
self.timer.stop()
self.timer.timeout.disconnect(self.startPressed) # Disconnect the connection
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.cw = CustomWidget()
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.cw)
self.setLayout(self.layout)
self.show()
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
app = QtWidgets.QApplication([])
win = App()
status = app.exec_()