想检测按键一直被按下去的状态该怎么写?

hawkingyy 发布于 2017/03/18 19:11
阅读 1K+
收藏 0

pyqt5做了一个pushbutton,当这个pushbutton一直被按下时,i不停自加怎么实现呢?

加载中
0
您的好友
您的好友

没写过这个  但是应该也差不多   除了click之外  应该有touchstart/touchend或者mousedown/mouseup类似这样的事件的吧   

0
reter
reter
import sys
from PyQt5.QtWidgets import QFrame,QPushButton,QLabel
from PyQt5.QtCore import QBasicTimer

class demo(QFrame):
    def __init__(self):
        super().__init__()
        self.button = QPushButton(self)
        self.button.setText('一个按钮')
        self.button.pressed.connect(self.onMouseDown)
        self.button.released.connect(self.onMouseUp)
        self.button.move(50,50)
        self.resetbtn = QPushButton(self)
        self.resetbtn.clicked.connect(self.resetCount)
        self.resetbtn.setText('重置')
        self.resetbtn.move(50,150)
        self.label = QLabel(self)
        self.label.move(150,50)
        self.count = 0;
        self.speed = 0.1;
        self.label.setText(str(self.count))
        self.timer = QBasicTimer()

    def updateLabel(self):
        self.label.setText(str(self.count))
        self.label.adjustSize()

    def onMouseDown(self):
        print('button is pressed')
        if not self.timer.isActive():
            self.timer.start(self.speed,self)

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

    def timerEvent(self,event): 
        self.count = self.count+1
        #print(self.count)
        self.updateLabel()
    
    def resetCount(self):
        self.count = 0
        self.updateLabel()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    d = demo()
    d.show()
    sys.exit(app.exec_())

主要是 QBasicTimer 和 timerEvent 的结合使用

0
h
hawkingyy

引用来自“南城小镇”的评论

import sys
from PyQt5.QtWidgets import QFrame,QPushButton,QLabel
from PyQt5.QtCore import QBasicTimer

class demo(QFrame):
    def __init__(self):
        super().__init__()
        self.button = QPushButton(self)
        self.button.setText('一个按钮')
        self.button.pressed.connect(self.onMouseDown)
        self.button.released.connect(self.onMouseUp)
        self.button.move(50,50)
        self.resetbtn = QPushButton(self)
        self.resetbtn.clicked.connect(self.resetCount)
        self.resetbtn.setText('重置')
        self.resetbtn.move(50,150)
        self.label = QLabel(self)
        self.label.move(150,50)
        self.count = 0;
        self.speed = 0.1;
        self.label.setText(str(self.count))
        self.timer = QBasicTimer()

    def updateLabel(self):
        self.label.setText(str(self.count))
        self.label.adjustSize()

    def onMouseDown(self):
        print('button is pressed')
        if not self.timer.isActive():
            self.timer.start(self.speed,self)

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

    def timerEvent(self,event): 
        self.count = self.count+1
        #print(self.count)
        self.updateLabel()
    
    def resetCount(self):
        self.count = 0
        self.updateLabel()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    d = demo()
    d.show()
    sys.exit(app.exec_())

主要是 QBasicTimer 和 timerEvent 的结合使用

完全可行!!!谢谢!!!

你给的例子是鼠标按键的,如果想检测键盘的“W”键一直被按下这种状态,应该用哪一种方法呢?

返回顶部
顶部