# _*_ coding:utf-8 _*_ import sys from PyQt4 import QtGui,QtCore class Example(QtGui.QMainWindow): def __init__(self): super(Example,self).__init__() self.setWindowTitle('Escape') self.initUI() self.resize(250,150) def initUI(self): button1=QtGui.QPushButton('button1',self) button2=QtGui.QPushButton('button2',self) button1.move(30,30) button2.move(30,100) self.connect(button1,QtCore.SIGNAL('clicked()'),self.buttonClicked) self.connect(button2, QtCore.SIGNAL('clicked()'), self.buttonClicked) self.statusBar().showMessage('Ready') def buttonClicked(self): sender=self.sender() self.statusBar().showMessage(sender.text()+'was pressed') def keyPressEvent(self,event): print event.key() print QtCore.Qt.Key_Escape if event.key()==QtCore.Qt.Key_Enter: self.close() app=QtGui.QApplication(sys.argv) ex=Example() ex.show() sys.exit(app.exec_())