PyQt5如何在QLineEdit中显示键输入?

时间:2019-05-29 13:38:08

标签: python pyqt

我试图在第二个窗口中获得一个只读的QLineEdit,以显示按下的键的名称。例如,如果我按下键a,则显示a Key;如果按下空格键,则显示SpaceBar Key。我在网上搜索解决方案,但大多数解决方案都是关于如何在按下某个键时关闭窗口。

我下面有一个用tkinter制作的工作样本。 https://gist.github.com/sangonkim/e397c420d93485f3896f9440bbcc77aa

我用QDesigner制作了两个简单的窗口。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(347, 259)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(110, 110, 121, 28))
        self.pushButton.clicked.connect(self.connect)
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)

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

    def connect(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow2()
        self.ui.setupUi(self.window)
        self.window.show()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "To next window"))


class Ui_MainWindow2(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(384, 261)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 21))
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Main_Window = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(Main_Window)
    Main_Window.show()
    sys.exit(app.exec_())

我尝试添加

def keyinput(self, event):
    print(event.key())

访问Ui_MainWindow2,查看我是否可以捕捉键入的密钥。但这不起作用。

也尝试过

def keyinput(self, event):
    self.lineEdit.setText(str(event.key()))

失败了。

1 个答案:

答案 0 :(得分:2)

进入setupUi添加:

MainWindow.keyPressEvent = self.newOnkeyPressEvent

并定义函数:

def newOnkeyPressEvent(self, event):

    key = event.key()
    modifiers = int(event.modifiers())
    keyname = QtGui.QKeySequence(modifiers + key).toString()
    print(keyname)
    self.lineEdit.setText(str(keyname))

完整代码:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(347, 259)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(110, 110, 121, 28))
        self.pushButton.clicked.connect(self.connect)
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)

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

    def connect(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow2()
        self.ui.setupUi(self.window)
        self.window.show()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "To next window"))



class Ui_MainWindow2(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(384, 261)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 21))
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)

        MainWindow.keyPressEvent = self.newOnkeyPressEvent

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


    def newOnkeyPressEvent(self, event):

        key = event.key()
        modifiers = int(event.modifiers())
        keyname = QtGui.QKeySequence(modifiers + key).toString()
        print(keyname)
        self.lineEdit.setText(str(keyname))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Main_Window = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(Main_Window)
    Main_Window.show()
    sys.exit(app.exec_())