zetcode :: First programs in PyQt5

练习代码,详见网站 http://zetcode.com/gui/pyqt5/firstprograms/

import sys
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore


class TooltipExample(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 静态方法,设置Tooltip字体
        QtWidgets.QToolTip.setFont(QtGui.QFont("SansSerif", 15))
        self.setToolTip("This is a <b>QWidget</b> widget")
        # tooltip持续1000毫秒
        self.setToolTipDuration(1000)

        btn = QtWidgets.QPushButton('Quit', self)
        # QCoreApplication.instance 返回当前实例
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn.resize(btn.sizeHint())
        btn.move(50, 50)
        btn.setToolTip("This is a <b>QPushButton</b> widget")
        btn.setToolTipDuration(1000)

        # x,y,width,height
        #self.setGeometry(300, 300, 300, 150)
        self.resize(300,150)
        self.center()
        self.setWindowTitle("Tooltips")

    def center(self):
        wr = self.frameGeometry()
        # QDesktopWidget::availableGeometry 返回屏幕rect
        qr = QtWidgets.QDesktopWidget().availableGeometry().center()
        # QRect:moveCenter 中心对齐
        wr.moveCenter(qr)
        # 实际调节窗口位置
        self.move(wr.topLeft())

    def closeEvent(self, event):
        # 静态方法生成MessageBox,返回StandardButton
        reply = QtWidgets.QMessageBox.question(self, 'Message', 'Are you sure to quit ?',
                                               QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
                                               QtWidgets.QMessageBox.No)

        if reply == QtWidgets.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    example = TooltipExample()
    example.show()
    sys.exit(app.exec_())

 

 

转载于:https://www.cnblogs.com/lkpp/p/7400034.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值