PyQt5中如何判断按钮是否被连续按下?

发布于 2025-01-11 05:31:28 字数 408 浏览 0 评论 0原文

对于我的应用,我需要只要按下按钮,我的升降系统就会上升,而当我不按下按钮时,它应该停止。

clicked() 函数不能用于此目的。然而,pressed() 和released() 函数也不起作用。

我在下面剪下了代码的相关部分。我的目标是只要按下按钮就打印“按下”文本

def __init__(self):
    manual_button = QPushButton('Lift Button')
    manual_button.pressed.connect(press_function)
    self.manual_grid.addWidget(manual_button, 0, 1)

def press_function(self):
    print('pressed')

谢谢

For my application, I need my lift system to go up as long as the button is pressed and it should stop when I don't press the button.

clicked() function is not functional for this purpose. However pressed() and released() functions also didn't work.

I snipped related section of my code below. My aim is to print "Pressed" text as long as button is pressed

def __init__(self):
    manual_button = QPushButton('Lift Button')
    manual_button.pressed.connect(press_function)
    self.manual_grid.addWidget(manual_button, 0, 1)

def press_function(self):
    print('pressed')

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2025-01-18 05:31:28

只需使用按下释放信号即可启动/停止QTimer。像...

#!/usr/local/bin/python3
import os
import sys
from PyQt5.QtCore import(QTimer)
from PyQt5.QtWidgets import(QApplication, QPushButton)

def button_pressed(timer):
    timer.start(100)

def button_released(timer):
    timer.stop()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    pb = QPushButton("Press")
    timer = QTimer()
    pb.pressed.connect(lambda checked = False: button_pressed(timer))
    pb.released.connect(lambda checked = False: button_released(timer))
    timer.timeout.connect(lambda: print('Button Pressed'))
    pb.show()
    app.exec_()

Just use the pressed and released signals to start/stop a QTimer. Something like...

#!/usr/local/bin/python3
import os
import sys
from PyQt5.QtCore import(QTimer)
from PyQt5.QtWidgets import(QApplication, QPushButton)

def button_pressed(timer):
    timer.start(100)

def button_released(timer):
    timer.stop()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    pb = QPushButton("Press")
    timer = QTimer()
    pb.pressed.connect(lambda checked = False: button_pressed(timer))
    pb.released.connect(lambda checked = False: button_released(timer))
    timer.timeout.connect(lambda: print('Button Pressed'))
    pb.show()
    app.exec_()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文