PyQt入门之添加退出按钮,添加悬浮信息,图标

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__() #调用父类构造函数初始化
        self.setWindowTitle("第一个窗口")
        self.resize(500,500)
        screen = QDesktopWidget().screenGeometry()  # 获取屏幕坐标系
        size=self.geometry()#获取窗口坐标系
        self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)#让窗口居中
        self.setToolTip("<h1>这是第一个窗口</h1>")#设置悬浮信息
        self.setWindowIcon(QIcon("img.png"))
        self.setupui()
    def setupui(self):
        label=QLabel(self)
        label.setText("第一个窗口")
        label.move(230,250)
        menu=self.menuBar()#默认有菜单栏对象
        m1=menu.addMenu("菜单")
        m1.addAction("功能1")
        m1.addAction("功能2")
        m1.addAction("功能3")
        tool=self.addToolBar("工具栏")#添加一个工具栏
        tool.addAction("工具1")
        tool.addAction("工具2")
        tool.addAction("工具3")
        state=self.statusBar()#默认自带一个工具栏对象
        state.showMessage("运行中")
        button=QPushButton(self)
        button.setText("退出程序")
        button.setToolTip("<h1><sub>你确定要关闭吗?</sub></h1>")
        button.move(0,50)
        button.clicked.connect(self.closebutton)
    def closebutton(self):
        app=QApplication.instance()
        app.quit()

if __name__ == '__main__':
    app=QApplication(sys.argv)
    window=MainWindow()
    window.show()
    sys.exit(app.exec_())

posted @ 2023-05-12 22:37  突破铁皮  阅读(291)  评论(0)    收藏  举报