Python 3.x keyPressEvent在我的PyQt应用程序中不工作

Python 3.x keyPressEvent在我的PyQt应用程序中不工作,python-3.x,pyqt5,Python 3.x,Pyqt5,我试图在使用QtDesigner生成的主窗口中使用keyPressEvent,但我的程序未检测到任何按键。这是我的密码 QtDesigner生成的类是MainWindowUI class MainWindow(QMainWindow,MainWindowUI.Ui_MainWindow): def __init__(self, translator,parent=None): QMainWindow.__init__(self, parent=parent)

我试图在使用QtDesigner生成的主窗口中使用keyPressEvent,但我的程序未检测到任何按键。这是我的密码 QtDesigner生成的类是MainWindowUI

class MainWindow(QMainWindow,MainWindowUI.Ui_MainWindow):

    def __init__(self, translator,parent=None):
        QMainWindow.__init__(self, parent=parent)
        #super(MainWindow,self).__init__(parent)
        self.setupUi(self)
        self.translator=translator
        self.completeGUI()


    def completeGUI(self):
        self.setConnections()
        self.set_line_edit_validators()
        self.category_combo.insertSeparator(4)
        self.category_combo.insertSeparator(8)
        self.type_conjug_combo.insertSeparator(3)
        self.type_conjug_combo.insertSeparator(6)
        #self.type_conjug_combo.setItemData( 0, QtGui.QColor('red'), QtCore.Qt.ForegroundRole )
        self.save_button.clicked.connect(self.fileSave)
        self.category_label.hide()
        self.category_combo.hide()
        self.type_conjug_label.hide()
        self.type_conjug_combo.hide()
        self.grammar_combo.setStyleSheet("color:red")
        self.category_combo.setStyleSheet("color:red;")
        self.type_conjug_combo.setStyleSheet("color:red;")
        self.palat_combo.setStyleSheet("color:red;")
        self.accent_combo.setStyleSheet("color:red;")
        self.terme_edit.setStyleSheet("border: 2px solid red;")
        print("completeGUI is over")

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


我在这个论坛上查看了模拟问题的各种答案,但我无法让它工作。 谢谢你的帮助

下面是一个再现问题的最小示例

from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication
import sys
from view.NewMainWindow import NewMainWindow



if __name__=='__main__':
        app = QApplication(sys.argv)
        translator = QtCore.QTranslator()

        app.installTranslator(translator)
        mainWindow = NewMainWindow(translator)
        NewMainWindow.show(mainWindow)
        #sys.exit(app.exec_())
        current_exit_code=app.exec_()
        app=None

还有新的主窗口

from PyQt5.QtWidgets import  QMainWindow
from gen import NewMainWindowUI 


class NewMainWindow(QMainWindow,NewMainWindowUI.Ui_MainWindow):

    def __init__(self,translator, parent=None):
        super(NewMainWindow,self).__init__(parent)       
        self.setupUi(self)
        print('init completed')

    def keyPressEvent(self, e):
        print('event detected')
        print(e.key())

生成的接口

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'src/designer/newmainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.13.1
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(130, 110, 113, 36))
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 29))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        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)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())



重载
keyPressEvent
时提供一个。您仍然需要返回
super()。keyPressEvent(e)
。这可能与您的实际问题无关,但像这样它根本不起作用。@TimKörner与
super
没有任何关系,这是因为问题的根源是概念性的:调用
super()。keyPressEvent(event)
不会改变任何东西,因为问题是QMainWindow的keyPressEvent方法一开始没有被调用,可能是因为中央小部件(或平台“插件”)使用了它。@eyllanesc请参阅最小可复制示例。只有return键触发事件。@meaulnes您的编辑不是一个可复制的示例:它需要从
gen
导入的
NewMainWindowUI.Ui\U MainWindow
类,您没有提供该类。当重载
keyPressEvent
时,请提供一个。您仍然需要返回
super()。keyPressEvent(e)
。这可能与您的实际问题无关,但像这样它根本不起作用。@TimKörner与
super
没有任何关系,这是因为问题的根源是概念性的:调用
super()。keyPressEvent(event)
不会改变任何东西,因为问题是QMainWindow的keyPressEvent方法一开始没有被调用,可能是因为中央小部件(或平台“插件”)使用了它。@eyllanesc请参阅最小可复制示例。只有return键触发事件。@meaulnes您的编辑不是一个可复制的示例:它需要从
gen
导入的
NewMainWindowUI.Ui\u MainWindow
类,您没有提供该类。