# PyQt中的事件处理主要依赖重写事件处理函数来实现 import sys from PyQt4 import QtCore, QtGui class MainWindow(QtGui.QWidget): def __init__(self, parent = None): QtGui.QWidget.__init__(self, parent) self.setWindowTitle('emit') self.resize(250, 150) self.connect(self, QtCore.SIGNAL('closeEmitApp'), QtCore.SLOT('close()')) # 重新实现了keyPressEvent()事件 def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: # 当按下Esc键时程序就会结束 self.close() app = QtGui.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())