# 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_())