keyPressEvent方法如何在此程序中运行?

Ci3*_*Ci3 9 pyqt

我无法理解keyPressEvent方法在此程序中的工作原理.具体来说,这里的"e"是什么?keyPressEvent是使用预先存在的实例"e"重新定义的方法吗?

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.setGeometry(300,300,250,150)
        self.setWindowTitle('Event handler')
        self.show()

    def keyPressEvent(self, e):

        if e.key() == QtCore.Qt.Key_Escape:
            self.close()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

Fra*_*Man 11

e是用户按下键时生成的"事件".这是事件处理程序很常见,它是传递的信息(如哪个键得到按下 - 这是何等的越来越拉带e.key())一个伟大的方式将事件处理程序,这样我们就可以结合相关的事件,并处理它们单一功能.

# A key has been pressed!
def keyPressEvent(self, event):
    # Did the user press the Escape key?
    if event.key() == QtCore.Qt.Key_Escape: # QtCore.Qt.Key_Escape is a value that equates to what the operating system passes to python from the keyboard when the escape key is pressed.
        # Yes: Close the window
        self.close()
    # No:  Do nothing.
Run Code Online (Sandbox Code Playgroud)