码迷,mamicode.com
首页 > 其他好文 > 详细

No.1 PyQt学习

时间:2017-10-16 00:17:40      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:center   http   ges   push   from   nss   技术   tle   ble   

由于项目的原因,要学PyQt了。以下是第一天的学习成果

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore


class 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 <b>QPushButton</b> 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 都会有消息框,如下:

技术分享

No.1 PyQt学习

标签:center   http   ges   push   from   nss   技术   tle   ble   

原文地址:http://www.cnblogs.com/farewell-farewell/p/7674838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!