keyPressEvent()方法不适用于PyQt5 / Python 3+

时间:2016-11-05 21:43:42

标签: python qt python-3.x pyqt5

我是python的新手。我使用“Qt Designer”设计了一个简单的计算器应用程序,然后使用“pyuic5”将“ui”文件转换为“py”。

但是当我添加了keyPressEvent()方法时,该方法无效。我不知道为什么。我尝试过使用setFocus()方法来解决这个问题但是失败了。

有人可以帮我吗?

请检查下面的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setMinimumSize(QtCore.QSize(330, 280))
        MainWindow.setMaximumSize(QtCore.QSize(330, 280))

        self.centralwidget = QtWidgets.QWidget(MainWindow)


        self.lineEdit_display = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_display.setGeometry(QtCore.QRect(10, 0, 311, 41))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_display.setFont(font)
        self.lineEdit_display.setMaxLength(15)
        self.lineEdit_display.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.lineEdit_display.setReadOnly(True)


        self.pButton_0 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_0.setGeometry(QtCore.QRect(10, 200, 51, 41))
        self.pButton_0.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

        self.pButton_00 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_00.setGeometry(QtCore.QRect(70, 200, 51, 41))

        self.pButton_dot = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_dot.setGeometry(QtCore.QRect(130, 200, 51, 41))

        self.pButton_1 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_1.setGeometry(QtCore.QRect(10, 150, 51, 41))

        self.pButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_2.setGeometry(QtCore.QRect(70, 150, 51, 41))

        self.pButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_3.setGeometry(QtCore.QRect(130, 150, 51, 41))

        self.pButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_4.setGeometry(QtCore.QRect(10, 100, 51, 41))

        self.pButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_5.setGeometry(QtCore.QRect(70, 100, 51, 41))

        self.pButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_6.setGeometry(QtCore.QRect(130, 100, 51, 41))

        self.pButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_7.setGeometry(QtCore.QRect(10, 50, 51, 41))

        self.pButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_8.setGeometry(QtCore.QRect(70, 50, 51, 41))

        self.pButton_9 = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_9.setGeometry(QtCore.QRect(130, 50, 51, 41))

        self.pButton_add = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_add.setGeometry(QtCore.QRect(210, 50, 51, 41))

        self.pButton_subtract = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_subtract.setGeometry(QtCore.QRect(210, 100, 51, 41))

        self.pButton_multiplication = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_multiplication.setGeometry(QtCore.QRect(210, 150, 51, 41))

        self.pButton_divide = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_divide.setGeometry(QtCore.QRect(210, 200, 51, 41))

        self.pButton_equal = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_equal.setGeometry(QtCore.QRect(270, 150, 51, 91))

        self.pButton_ac = QtWidgets.QPushButton(self.centralwidget)
        self.pButton_ac.setGeometry(QtCore.QRect(270, 50, 51, 91))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.pButton_ac.setFont(font)
        self.pButton_ac.setDefault(True)


        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 330, 27))
        self.menuMain = QtWidgets.QMenu(self.menubar)
        MainWindow.setMenuBar(self.menubar)
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionAbout = QtWidgets.QAction(MainWindow)
        self.menuMain.addAction(self.actionExit)
        self.menuMain.addAction(self.actionAbout)
        self.menubar.addAction(self.menuMain.menuAction())

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Simple Calculator"))
        MainWindow.setToolTip(_translate("MainWindow", "Simple Calculator"))
        self.pButton_0.setToolTip(_translate("MainWindow", "0"))
        self.pButton_0.setText(_translate("MainWindow", "0"))
        self.pButton_00.setToolTip(_translate("MainWindow", "00"))
        self.pButton_00.setText(_translate("MainWindow", "00"))
        self.pButton_dot.setToolTip(_translate("MainWindow", "."))
        self.pButton_dot.setText(_translate("MainWindow", "."))
        self.pButton_1.setToolTip(_translate("MainWindow", "1"))
        self.pButton_1.setText(_translate("MainWindow", "1"))
        self.pButton_2.setToolTip(_translate("MainWindow", "2"))
        self.pButton_2.setText(_translate("MainWindow", "2"))
        self.pButton_3.setToolTip(_translate("MainWindow", "3"))
        self.pButton_3.setText(_translate("MainWindow", "3"))
        self.pButton_4.setToolTip(_translate("MainWindow", "4"))
        self.pButton_4.setText(_translate("MainWindow", "4"))
        self.pButton_5.setToolTip(_translate("MainWindow", "5"))
        self.pButton_5.setText(_translate("MainWindow", "5"))
        self.pButton_6.setToolTip(_translate("MainWindow", "6"))
        self.pButton_6.setText(_translate("MainWindow", "6"))
        self.pButton_7.setToolTip(_translate("MainWindow", "7"))
        self.pButton_7.setText(_translate("MainWindow", "7"))
        self.pButton_8.setToolTip(_translate("MainWindow", "8"))
        self.pButton_8.setText(_translate("MainWindow", "8"))
        self.pButton_9.setToolTip(_translate("MainWindow", "9"))
        self.pButton_9.setText(_translate("MainWindow", "9"))
        self.pButton_add.setToolTip(_translate("MainWindow", "+"))
        self.pButton_add.setText(_translate("MainWindow", "+"))
        self.pButton_subtract.setToolTip(_translate("MainWindow", "-"))
        self.pButton_subtract.setText(_translate("MainWindow", "-"))
        self.pButton_multiplication.setToolTip(_translate("MainWindow", "\N{MULTIPLICATION SIGN}"))
        self.pButton_multiplication.setText(_translate("MainWindow", "\N{MULTIPLICATION SIGN}"))
        self.pButton_divide.setToolTip(_translate("MainWindow", "\N{DIVISION SIGN}"))
        self.pButton_divide.setText(_translate("MainWindow", "\N{DIVISION SIGN}"))
        self.pButton_equal.setToolTip(_translate("MainWindow", "="))
        self.pButton_equal.setText(_translate("MainWindow", "="))
        self.pButton_ac.setToolTip(_translate("MainWindow", "AC"))
        self.pButton_ac.setText(_translate("MainWindow", "AC"))
        self.lineEdit_display.setToolTip(_translate("MainWindow", "Display"))
        self.lineEdit_display.setText(_translate("MainWindow", "0"))
        self.menuMain.setTitle(_translate("MainWindow", "Mai&n"))
        self.actionExit.setText(_translate("MainWindow", "&Exit"))
        self.actionAbout.setText(_translate("MainWindow", "&About"))

    def keyPressEvent(self, e):
        print(e.key())

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)

    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    MainWindow.setFocus() #TRYING TO SET FOCUS FOR keyPressEvent

    sys.exit(app.exec_())

3 个答案:

答案 0 :(得分:1)

看起来您需要将每个小部件与事件相关联。 GUI是事件驱动的编程。正如您所见,他们只是坐在那里看着您,直到用户采取某些行动,并且您(程序员)有责任定义每个动作的结果。

例如,您有计算器的按钮。我假设当您按下数字按钮时,您希望该数字显示在表示显示的文本框中。如果是这样,那么您需要处理“点击”事件。类似于

的东西
 self.pButton_9.clicked.connect(self.click_9) # connects button to action by defining a callback - an action to handle the event.

def click_9(self):
    self.lineEdit_display.text += '9 '

我见过的最好的介绍教程之一是Here。我通常都会通过它来开始学习GUI编程。

答案 1 :(得分:1)

先生,我无法找到具体的语法错误,因此我编辑了您的代码,以尽可能与您的代码保持相似。

这是您的代码使用了一些修改,看看它是否适合您。

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMenu
from PyQt5.QtWidgets import QMenuBar
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget


class Ui_MainWindow(QMainWindow):

    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        # self.setupUi()

    def setupUi(self):
        self.setMinimumSize(QtCore.QSize(330, 280))
        self.setMaximumSize(QtCore.QSize(330, 280))

        self.centralwidget = QWidget()


        self.lineEdit_display = QLineEdit(self.centralwidget)
        self.lineEdit_display.setGeometry(QtCore.QRect(10, 0, 311, 41))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_display.setFont(font)
        self.lineEdit_display.setMaxLength(15)
        self.lineEdit_display.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.lineEdit_display.setReadOnly(True)


        self.pButton_0 = QPushButton(self.centralwidget)
        self.pButton_0.setGeometry(QtCore.QRect(10, 200, 51, 41))
        self.pButton_0.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

        self.pButton_00 = QPushButton(self.centralwidget)
        self.pButton_00.setGeometry(QtCore.QRect(70, 200, 51, 41))

        self.pButton_dot = QPushButton(self.centralwidget)
        self.pButton_dot.setGeometry(QtCore.QRect(130, 200, 51, 41))

        self.pButton_1 = QPushButton(self.centralwidget)
        self.pButton_1.setGeometry(QtCore.QRect(10, 150, 51, 41))

        self.pButton_2 = QPushButton(self.centralwidget)
        self.pButton_2.setGeometry(QtCore.QRect(70, 150, 51, 41))

        self.pButton_3 = QPushButton(self.centralwidget)
        self.pButton_3.setGeometry(QtCore.QRect(130, 150, 51, 41))

        self.pButton_4 = QPushButton(self.centralwidget)
        self.pButton_4.setGeometry(QtCore.QRect(10, 100, 51, 41))

        self.pButton_5 = QPushButton(self.centralwidget)
        self.pButton_5.setGeometry(QtCore.QRect(70, 100, 51, 41))

        self.pButton_6 = QPushButton(self.centralwidget)
        self.pButton_6.setGeometry(QtCore.QRect(130, 100, 51, 41))

        self.pButton_7 = QPushButton(self.centralwidget)
        self.pButton_7.setGeometry(QtCore.QRect(10, 50, 51, 41))

        self.pButton_8 = QPushButton(self.centralwidget)
        self.pButton_8.setGeometry(QtCore.QRect(70, 50, 51, 41))

        self.pButton_9 = QPushButton(self.centralwidget)
        self.pButton_9.setGeometry(QtCore.QRect(130, 50, 51, 41))

        self.pButton_add = QPushButton(self.centralwidget)
        self.pButton_add.setGeometry(QtCore.QRect(210, 50, 51, 41))

        self.pButton_subtract = QPushButton(self.centralwidget)
        self.pButton_subtract.setGeometry(QtCore.QRect(210, 100, 51, 41))

        self.pButton_multiplication = QPushButton(self.centralwidget)
        self.pButton_multiplication.setGeometry(QtCore.QRect(210, 150, 51, 41))

        self.pButton_divide = QPushButton(self.centralwidget)
        self.pButton_divide.setGeometry(QtCore.QRect(210, 200, 51, 41))

        self.pButton_equal = QPushButton(self.centralwidget)
        self.pButton_equal.setGeometry(QtCore.QRect(270, 150, 51, 91))

        self.pButton_ac = QPushButton(self.centralwidget)
        self.pButton_ac.setGeometry(QtCore.QRect(270, 50, 51, 91))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.pButton_ac.setFont(font)
        self.pButton_ac.setDefault(True)

        self.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 330, 27))
        self.menuMain = QMenu(self.menubar)
        self.setMenuBar(self.menubar)
        self.actionExit = QAction(self)
        self.actionAbout = QAction(self)
        self.menuMain.addAction(self.actionExit)
        self.menuMain.addAction(self.actionAbout)
        self.menubar.addAction(self.menuMain.menuAction())

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

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("MainWindow", "Simple Calculator"))
        self.setToolTip(_translate("MainWindow", "Simple Calculator"))
        self.pButton_0.setToolTip(_translate("MainWindow", "0"))
        self.pButton_0.setText(_translate("MainWindow", "0"))
        self.pButton_00.setToolTip(_translate("MainWindow", "00"))
        self.pButton_00.setText(_translate("MainWindow", "00"))
        self.pButton_dot.setToolTip(_translate("MainWindow", "."))
        self.pButton_dot.setText(_translate("MainWindow", "."))
        self.pButton_1.setToolTip(_translate("MainWindow", "1"))
        self.pButton_1.setText(_translate("MainWindow", "1"))
        self.pButton_2.setToolTip(_translate("MainWindow", "2"))
        self.pButton_2.setText(_translate("MainWindow", "2"))
        self.pButton_3.setToolTip(_translate("MainWindow", "3"))
        self.pButton_3.setText(_translate("MainWindow", "3"))
        self.pButton_4.setToolTip(_translate("MainWindow", "4"))
        self.pButton_4.setText(_translate("MainWindow", "4"))
        self.pButton_5.setToolTip(_translate("MainWindow", "5"))
        self.pButton_5.setText(_translate("MainWindow", "5"))
        self.pButton_6.setToolTip(_translate("MainWindow", "6"))
        self.pButton_6.setText(_translate("MainWindow", "6"))
        self.pButton_7.setToolTip(_translate("MainWindow", "7"))
        self.pButton_7.setText(_translate("MainWindow", "7"))
        self.pButton_8.setToolTip(_translate("MainWindow", "8"))
        self.pButton_8.setText(_translate("MainWindow", "8"))
        self.pButton_9.setToolTip(_translate("MainWindow", "9"))
        self.pButton_9.setText(_translate("MainWindow", "9"))
        self.pButton_add.setToolTip(_translate("MainWindow", "+"))
        self.pButton_add.setText(_translate("MainWindow", "+"))
        self.pButton_subtract.setToolTip(_translate("MainWindow", "-"))
        self.pButton_subtract.setText(_translate("MainWindow", "-"))
        self.pButton_multiplication.setToolTip(_translate("MainWindow", "\N{MULTIPLICATION SIGN}"))
        self.pButton_multiplication.setText(_translate("MainWindow", "\N{MULTIPLICATION SIGN}"))
        self.pButton_divide.setToolTip(_translate("MainWindow", "\N{DIVISION SIGN}"))
        self.pButton_divide.setText(_translate("MainWindow", "\N{DIVISION SIGN}"))
        self.pButton_equal.setToolTip(_translate("MainWindow", "="))
        self.pButton_equal.setText(_translate("MainWindow", "="))
        self.pButton_ac.setToolTip(_translate("MainWindow", "AC"))
        self.pButton_ac.setText(_translate("MainWindow", "AC"))
        self.lineEdit_display.setToolTip(_translate("MainWindow", "Display"))
        self.lineEdit_display.setText(_translate("MainWindow", "0"))
        self.menuMain.setTitle(_translate("MainWindow", "Mai&n"))
        self.actionExit.setText(_translate("MainWindow", "&Exit"))
        self.actionAbout.setText(_translate("MainWindow", "&About"))

    def keyPressEvent(self, e):
        print(e.key())

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)

    ui = Ui_MainWindow()
    ui.setupUi()
    ui.show()

    sys.exit(app.exec_())

希望它有效^^

答案 2 :(得分:0)

我也有这个问题。 在 setupUI 函数中使用此代码:

MainWindow.keyPressEvent = self.keyPressEvent