首页 > 代码库 > pyqt重写键盘事件+获取信号发送对象

pyqt重写键盘事件+获取信号发送对象

# _*_ coding:utf-8 _*_import sysfrom PyQt4 import QtGui,QtCoreclass 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_())

 

pyqt重写键盘事件+获取信号发送对象