kaun
kaun

Reputation: 21

How to run my function only when button is pressed and held with pyqt5 in python

How can I make a Button which executes the code under it only when the Button is pressed and held (let's say for one second) and stops when it is released?

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button '
        self.left = 500
        self.top = 200
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100, 70)
        button.clicked.connect(self.on_click)

        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_()) ```

Upvotes: 0

Views: 1512

Answers (1)

Heike
Heike

Reputation: 24420

You don't mention what kind of function you want to execute while the button is pressed, but suppose you want to execute a function every second or so while the button is pressed.

In that case you could use a QTimer and connect the function you want to execute to the QTimer.timeout signal.

You could then use the button.pressed and button.released signals to start and stop a timer.

For example:

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QProgressBar
from PyQt5.QtCore import QTimer


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button '
        self.left = 500
        self.top = 200
        self.width = 320
        self.height = 200
        self.initUI()

        self.counter = 0
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.on_timeout)

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        vlayout = QVBoxLayout(self)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')

        # connect slots
        button.pressed.connect(self.on_press)
        button.released.connect(self.on_release)

        self.progress = QProgressBar(self)
        self.progress.setValue(0)

        vlayout.addWidget(button)
        vlayout.addWidget(self.progress)

        self.show()

    # function that should be run periodically while button is pressed
    def on_timeout(self):
        self.counter += 1
        self.progress.setValue(self.counter)

    def on_press(self):
        self.progress.setValue(0)
        self.counter = 0
        self.timer.start(100)

    def on_release(self):
        self.timer.stop()


if __name__ == '__main__':
    app = QApplication([])
    ex = App()
    app.exec()

Upvotes: 1

Related Questions