PyQt5开始入门

1、使用 Qt designer

查看 pyQt5 + pycharm 配置安装PyQt5PyQt5-tools;使用Qt designer生成ui文件并用PyUic工具转换成py文件
在这里插入图片描述

1.1 配置Qt designer外部工具:两种方式

  1. pyqt5-tools.exe :pyqt5-tools.exe designer
    在这里插入图片描述

在这里插入图片描述
2. designer.exe
在这里插入图片描述
在这里插入图片描述

1.2 配置PyUIC外部工具

在这里插入图片描述
在这里插入图片描述

1.3 添加入口main运行结果

import sys

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

在这里插入图片描述

2、简单的窗口

QtWidgets.QApplication(sys.argv) 提供了整个图形界面程序的底层管理功能,比如:
初始化、程序入口参数的处理,用户事件(对界面的点击、输入、拖拽)分发给各个对应的控件,等等…

  • 每个PyQt5应用都必须创建一个应用对象。sys.argv是一组命令行参数的列表。Python可以在shell里运行,这个参数提供对脚本控制的功能。
  • app.exec_() 进入了应用的主循环中,事件处理器这个时候开始工作。主循环从窗口上接收事件,并把事件派发到应用控件里。当调用exit()方法或直接销毁主控件时,主循环就会结束。sys.exit()方法能确保主循环安全退出。外部环境会收到主控件如何结束的信息。

QtWidgets.QMainWindow() 界面的主窗口,继承QMainWindow(QWidget)MainWindow.show()能让控件在桌面上显示出来;QtWidgets.QWidget(MainWindow) QWidge控件是一个用户界面的基本控件,它提供了基本的应用构造器。默认情况下,构造器是没有父级的,没有父级的构造器被称为窗口(window)。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'app.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        self.menu_2 = QtWidgets.QMenu(self.menubar)
        self.menu_2.setObjectName("menu_2")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menu.menuAction())
        self.menubar.addAction(self.menu_2.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menu.setTitle(_translate("MainWindow", "打开"))
        self.menu_2.setTitle(_translate("MainWindow", "新建"))

2.1 带窗口图标

窗口图标通常是显示在窗口的左上角,标题栏的最左边。准备图片icon.ico,创建一个QIcon对象,设置左上角图标MainWindow.setWindowIcon()setWindowTitle设置左上角标题;上面的两个方法都继承自QWidget类

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        # ... ...
        MainWindow.setWindowTitle("XhNote")
        icon = QIcon("icons/icon.ico")
        MainWindow.setWindowIcon(icon)
        # ... ...

在这里插入图片描述

2.2 提示框

QToolTip.setFont(QFont('SansSerif', 10)) 这个静态方法设置了提示框的字体,我们使用了10px的SansSerif字体。
mainQwidget.setToolTip('This is a <b>QWidget</b> widget') 中央widget鼠标悬停提示
btn.setToolTip('This is a <b>QPushButton</b> widget') 按钮鼠标悬停提示

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtWidgets import QPushButton, QToolTip


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        # ... ...
        mainQwidget = QtWidgets.QWidget(MainWindow)
        QToolTip.setFont(QFont('SansSerif', 10))
        mainQwidget.setToolTip('This is a <b>QWidget</b> widget')
        btn = QPushButton('Button', mainQwidget)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)
        mainQwidget.setWindowTitle('Tooltips')
        #
        self.centralwidget = mainQwidget
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        # ... ...

在这里插入图片描述在这里插入图片描述

2.3 关闭窗口

qbtn.clicked.connect(QCoreApplication.instance().quit) 事件传递系统在PyQt5内建的single和slot机制里面。点击按钮之后,信号会被捕捉并给出既定的反应。QCoreApplication包含了事件的主循环,它能添加和删除所有的事件,instance()创建了一个它的实例。QCoreApplication是在QApplication里创建的。 点击事件和能终止进程并退出应用的quit函数绑定在了一起。在发送者和接受者之间建立了通讯,发送者就是按钮,接受者就是应用对象。

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QPushButton


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        # ... ...
        mainQwidget = QtWidgets.QWidget(MainWindow)
        qbtn = QPushButton('退出', mainQwidget)
        qbtn.clicked.connect(QCoreApplication.instance().quit)
        #
        self.centralwidget = mainQwidget
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        # ... ...

在这里插入图片描述

2.4 消息盒子

默认情况下,我们点击标题栏的×按钮,QWidget就会关闭。但是有时候,我们修改默认行为。比如,如果我们打开的是一个文本编辑器,并且做了一些修改,我们就会想在关闭按钮的时候让用户进一步确认操作。

如果关闭QWidget,就会产生一个QCloseEvent,并且把它传入到closeEvent函数的event参数中。改变控件的默认行为,就是替换掉默认的事件处理。
我们创建了一个消息框QMessageBox.question(),上面有俩按钮:Yes和No.第一个字符串显示在消息框的标题栏,第二个字符串显示在对话框,第三个参数是消息框的俩按钮,最后一个参数是默认按钮,这个按钮是默认选中的。返回值在变量reply里。

import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication, QDesktopWidget


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Message box')
        self.center()
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
                                     "Are you sure to quit?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)

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


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

2.5 窗口居中

QtGui.QDesktopWidget提供了用户的桌面信息,包括屏幕的大小。qr = self.frameGeometry() 获得主窗口所在的框架。cp = QDesktopWidget().availableGeometry().center() 获取显示器的分辨率,然后得到屏幕中间点的位置。qr.moveCenter(cp) 然后把主窗口框架的中心点放置到屏幕的中心位置。self.move(qr.topLeft()) 然后通过move函数把主窗口的左上角移动到其框架的左上角,这样就把窗口居中了。
qr.moveCenter(cp)注释后窗口就在屏幕左上角显示)

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QPushButton, QDesktopWidget


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        # ... ...
        self.center(MainWindow)


    def center(self, MainWindow):
        qr = MainWindow.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        MainWindow.move(qr.topLeft())

猜你喜欢

转载自blog.csdn.net/qq_23452385/article/details/131856052