2

我想创建一个 PyQt5 窗口(Windows 操作系统),它可以识别按住 CTRL 按钮的按钮单击。我成功创建了一个可以识别 CTRL 键按下的处理程序,但它找不到我需要调用和关闭按钮单击事件的按钮的按下和释放。我做了很多搜索,但 PyQt5 的资源似乎很低。任何帮助表示赞赏:)

import time
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)
        self.show()
        self.signals()
        self.bleahOK=True

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(392, 255)
        self.unlockButton = QtWidgets.QPushButton(Dialog)
        self.unlockButton.setGeometry(QtCore.QRect(10, 180, 171, 51))
        self.unlockButton.setObjectName("unlockButton")
        self.lockButton = QtWidgets.QPushButton(Dialog)
        self.lockButton.setGeometry(QtCore.QRect(220, 180, 151, 51))
        self.lockButton.setObjectName("lockButton")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 30, 331, 71))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(30, 120, 261, 31))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.unlockButton.setText(_translate("Dialog", "OK"))
        self.lockButton.setText(_translate("Dialog", "Lock"))
        self.label.setText(_translate("Dialog", ""))
        self.lineEdit.setText(_translate("Dialog", ""))

    def signals(self):
        self.unlockButton.clicked.connect(self.unlock)

    def unlock(self):
        if 1: print('ff')            

    def keyPressEvent(self, event):
         if type(event) == QtGui.QKeyEvent: #Unable to find when the key was released
            print (event.key())          
            event.accept()
         else:
             event.ignore()     

    def lock(self):
        print("Test")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    # ui.signals()
    # Dialog.show()
    sys.exit(app.exec_())
4

1 回答 1

1

我建议您不要修改 Qt Designer 生成的代码,而是创建一个继承适当小部件的类并将该类用作我推荐 PyQt 的接口。说到问题,你必须使用 keyReleaseEvent 方法来监听一个键被释放的时间:

import time
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(392, 255)
        self.unlockButton = QtWidgets.QPushButton(Dialog)
        self.unlockButton.setGeometry(QtCore.QRect(10, 180, 171, 51))
        self.unlockButton.setObjectName("unlockButton")
        self.lockButton = QtWidgets.QPushButton(Dialog)
        self.lockButton.setGeometry(QtCore.QRect(220, 180, 151, 51))
        self.lockButton.setObjectName("lockButton")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 30, 331, 71))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(30, 120, 261, 31))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.unlockButton.setText(_translate("Dialog", "OK"))
        self.lockButton.setText(_translate("Dialog", "Lock"))
        self.label.setText(_translate("Dialog", ""))
        self.lineEdit.setText(_translate("Dialog", ""))

class Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        self.is_key_ctrl_pressed = False
        self.unlockButton.clicked.connect(self.unlock)

    @QtCore.pyqtSlot()
    def unlock(self):
        if self.is_key_ctrl_pressed:
            print("unlock")

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Control:
            self.is_key_ctrl_pressed = True
        super(Dialog, self).keyPressEvent(event)

    def keyReleaseEvent(self, event):
        if event.key() == QtCore.Qt.Key_Control:
            self.is_key_ctrl_pressed = False
        super(Dialog, self).keyReleaseEvent(event)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())
于 2019-03-05T06:16:23.340 回答