当前位置:   article > 正文

PyQt5入门:一篇就够

pyqt5

PyQt5入门

(一)、窗口基本功能

1、第一个程序

编写一个PyQt5程序必须使用两个类:QApplicationQWidget ,这两个类都在PyQt5.QtWidgets模块中,所以首先要导入这个模块。

  • QApplication 类的实例表示整个应用程序,该类得构造方法需要传入Python程序得命令行参数(需要导入sys模块)
  • QWidget 类的实例相当于一个窗口
  • show 方法显示串口
import sys
from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == "__main__":
    # 创建QApplication类的实例,并传入命令行参数
    app = QApplication(sys.argv)
    # 创建QWidget类的实例,相当于创建一个窗口
    w = QWidget()
    # 调整窗口的大小(宽,高)
    w.resize(500, 300)
    # 移动窗口(显示的相对位置,左,上)
    w.move(100, 200)
    # 设置窗口的标题
    w.setWindowTitle("this is a pyqt5 window")
    # 显示窗口
    w.show()
    # 进入循环的主循环,并通过exit函数确保主循环安全结束
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2、为窗口添加图标

import sys
from PyQt5.QtWidgets import QApplication, QWidget
# 导入QIcon类,用于装载图像文件
from PyQt5.QtGui import QIcon


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = QWidget()
    # 设置窗口 位置(左,上) + 尺寸(宽,高)
    w.setGeometry(200, 100, 500, 300)
    w.setWindowTitle("图标效果")
    # 设置窗口图标
    w.setWindowIcon(QIcon("./icon.png"))
    w.show()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3、鼠标悬浮显示提示框

  • 显示提示框需要使用 QWidget类的setToolTip方法,需要引入QToolTip
  • setToolTip 方法接收一个字符串类型的参数值,作为提示框显示文本;将鼠标放置在设置的区域不动,1秒后显示提示框,如果鼠标不动,提示框会在几秒后消失
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton
# 引入QFont类,用于设置字体和字号
from PyQt5.QtGui import QFont

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = QWidget()
    w.setGeometry(200, 200, 500, 500)
    w.setWindowTitle("显示提示框")
    # 设置提示框中文本的字体和大小
    QToolTip.setFont(QFont("SansSerif", 20))
    # 为窗口设置提示框
    w.setToolTip("这是空白窗口")

    # 创建一个按钮(按键名称,按键所属窗口)
    btn = QPushButton("Button", w)
    # 为按键设置提示框
    btn.setToolTip("这是一个按键")
    # 设置按键大小,btn.sizeHint()返回按键最佳大小,也可以输入(宽,高)
    btn.resize(btn.sizeHint())
    # 设置按键位置(窗口左边,窗口上边)
    btn.move(100, 200)

    w.show()
    sys.exit(app.exec_())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

4、关闭窗口

QCoreApplication类是一个基础的应用程序类,它提供了Qt应用程序的事件循环、命令行参数处理、消息翻译和应用程序版本号等基本功能。它是PyQt5应用程序的基础,可以用来实现很多不同类型的应用程序。

  • 关闭窗口可以使用系统内置的quit方法
  • 如果要通过按键关闭窗口,可以间按键的单击事件与quit绑定
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
# 绑定事件基于QCoreApplication,也可基于QApplication
from PyQt5.QtCore import QCoreApplication


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = QWidget()
    w.setWindowTitle("关闭窗口事件绑定")
    w.setGeometry(200, 200, 500, 500)
    btn_quit = QPushButton("退出", w)
    # 按键绑定quit事件
    btn_quit.clicked.connect(QCoreApplication.instance().quit)
    # 写法2:btn_quit.clicked.connect(QApplication.instance().quit)
    btn_quit.resize(btn_quit.sizeHint())
    btn_quit.move(200, 200)

    w.show()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

5、消息盒子

消息盒子(MessageBox)其实就是各种类型的消息对话框,如消息对话框、警告对话框、询问对话框等

  • 主要组成:图标,提示信息,按键

QMessageBox类提供了若干个静态方法可以显示各种类型的对话框

  • information方法用于显示信息对话框
  • warning方法用于显示警告对话框
  • question方法用于显示询问对话框
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox


class MessageBox(QWidget):
    def __init__(self):
        super().__init__()
        # 初始化窗口
        self.initWindow()

    def initWindow(self):
        self.setGeometry(200, 200, 500, 300)
        self.setWindowTitle("消息盒子")
        self.show()

    # pyqt5 默认方法,这里是重写closeEvent方法
    def closeEvent(self, event):
        # 显示询问对话框(窗口,标题,询问框内容,询问框含有的按键,默认选择的按键)
        reply = QMessageBox.question(self, "消息", "确定要退出吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        # 不加以下判断,不管选择yes还是no都会关闭窗口
        if reply == QMessageBox.Yes:
            # 调用event的accept方法关闭窗口
            event.accept()
        else:
            # 调用event的ignore方法取消关闭窗口
            event.ignore()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = MessageBox()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

6、窗口居中

import sys
from PyQt5.QtWidgets import QApplication, QWidget


class CenterWindow(QWidget):
    def __init__(self):
        super(CenterWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.resize(500, 300)
        # 调用center方法将窗口在屏幕中心显示
        self.center()
        self.setWindowTitle("窗口居中")
        self.show()

    def center(self):
        desktop = app.desktop()
        # 需要int类型的数据,float可能在后续版本会删除
        self.move(int((desktop.width() - self.width()) / 2), int((desktop.height() - self.height()) / 2))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = CenterWindow()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 代码改进(将app写入类中)
import sys
from PyQt5.QtWidgets import QApplication, QWidget


class CenterWindow(QWidget):
    def __init__(self, app):
        super(CenterWindow, self).__init__()
        self.app = app
        self.initUI()

    def initUI(self):
        self.resize(500, 300)
        # 调用center方法将窗口在屏幕中心显示
        self.center()
        self.setWindowTitle("窗口居中")
        self.show()

    def center(self):
        desktop = self.app.desktop()
        # 需要int类型的数据,float可能在后续版本会删除
        self.move(int((desktop.width() - self.width()) / 2), int((desktop.height() - self.height()) / 2))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = CenterWindow(app)
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

(二)、布局

1、绝对布局

绝对布局非常灵活,但是又局限性

  • 不会随着窗口的变化而变化
  • 无法使用不同平台和不同分辨率的显示器
  • 更改字体大小会破坏布局
  • 重构时,需要对每个控件重新设置
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel


class AbsoluteLayout(QWidget):
    def __init__(self):
        super(AbsoluteLayout, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(200
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/913521
推荐阅读
相关标签
  

闽ICP备14008679号