由于项目的原因,要学PyQt了。以下是第一天的学习成果
# -*- coding: utf-8 -*-import sysfrom PyQt4 import QtGui, QtCoreclass Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10)) # 设置一个用来提示消息的字体,10px字体SansSerif btn = QtGui.QPushButton('Sure', self) btn.setToolTip('This is a QPushButton widget') btn.setGeometry(50, 110, 60, 25) qbtn = QtGui.QPushButton('Quit', self) qbtn.setGeometry(170, 110, 60, 25) #self.connect(qbtn, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()')) #qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit) qbtn.clicked.connect(self.qbtnClick) self.resize( 250, 150) self.center() self.setWindowTitle('Test') self.setWindowIcon(QtGui.QIcon('flat.png')) self.show() def qbtnClick(self): reply = QtGui.QMessageBox.question(self, 'Message', 'Are you sure to quit?', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: quit() def center(self): qr = self.frameGeometry() #得到该主窗口的矩形框架qr cp = QtGui.QDesktopWidget().availableGeometry().center() #屏幕中间点的坐标cp qr.moveCenter(cp) #将矩形框架移至屏幕正中央 self.move(qr.topLeft()) #应用窗口移至矩形框架的左上角点 def closeEvent(self, event): reply = QtGui.QMessageBox.question(self, 'Message', 'Are you sure to quit?', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: event.accept() else: event.ignore()def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main()
运行截图:
点击 × 和 Quit 都会有消息框,如下: