PyQt5--EventHandler
1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 14, 2018 4 5 @author: SaShuangYiBing 6 ''' 7 import sys 8 from PyQt5.QtCore import Qt 9 from PyQt5.QtWidgets import QApplication,QWidget 10 11 class New_test(QWidget): 12 def __init__(self): 13 super().__init__() 14 self.initUI() 15 16 def initUI(self): 17 self.setGeometry(300,300,250,150) 18 self.setWindowTitle('Event Handler') 19 self.show() 20 21 def keyPressEvent(self, e): 22 if e.key() == Qt.Key_Escape: 23 self.close() 24 25 if __name__ == '__main__': 26 app = QApplication(sys.argv) 27 ex = New_test() 28 sys.exit(app.exec_())
程序运行后窗口打开,此时按 Ctrl+Q 组合键即可关闭该窗口