PyQt5 —— QtDesigner,pyqt5-tools 结合

Qt 使用可以用 QtDesigner 创建 .ui 文件,并在 QtCreator 中自动生成C++文件,从C++中调用;PyQt 也可以,需要借助 PyQt 提供的 pyuic5 脚本。

工具准本

  1. QtDesigner
    pip3 install PyQt5-tools
  2. 安装 PyQt5,包含 pyuic 模块
    pip3 install PyQt5

正确的步骤

  1. 创建 .ui 文件
    打开 QtDesigner,新建,创建一个简单的界面文件,保存为 foo.ui
image.png
image.png
  1. 转换 .ui 文件为 py 文件
pyuic5 foo.ui -o ui_foo.py
  1. 使用生成的文件
    新建一个 python 源码文件, bar.py
  • 第一种方法: 组合ui
from PyQt5.QtWidgets import QDialog
from ui_foo import Ui_MainWindow

class MainWindow(QDialog):
    def __init__(self):
        super(MainWindow, self).__init__()

        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # Make some local modifications.
        self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")

        # Connect up the buttons.
        self.ui.okButton.clicked.connect(self.accept)
        self.ui.cancelButton.clicked.connect(self.reject)
  • 第二种方法: 多继承
from PyQt5.QtGui import QDialog
from ui_foo import Ui_MainWindow

class MainWindow(QDialog, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        # Set up the user interface from Designer.
        self.setupUi(self)

        # Make some local modifications.
        self.colorDepthCombo.addItem("2 colors (1 bit per pixel)")

        # Connect up the buttons.
        self.okButton.clicked.connect(self.accept)
        self.cancelButton.clicked.connect(self.reject)

结合 pycharm

pycharm 支持 ”external tool" 可以添加自定义的命令到 pycharm 菜单;利用这个,把 pyuic 添加进来,提高效率
settings->tools->external tools


image.png

资源文件 qrc

  • 用 QtCreator 或 QtDesigner 编辑 qrc
QtCreator 编辑 qrc

前缀 是 qrc 中用来分组的一个层级,在源码中体现为路径
在前缀下 添加文件:图片等
语言 是由于国际化的一个后缀(不同语言自动识别使用资源)

  • PyQt5 提供工具把 qrc 及其关联的文件编译为一个 py 文本,只需要引用这个文本,就可以使用资源。
    pyrcc5.exe image.qrc -o rc_image.py

  • 使用编译后的模块
    资源路径格式 ":前缀/文件名" 或 ":前缀/别名"

资源文件和别名
from rc_image import *
img = QPixmap(":/img/glyphicons-281-settings.png")    # 从编译后的资源文件中导入(文件名)
img = QPixmap(":/img/settings")    # 从编译后的资源文件中导入(别名)

demo 地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,999评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,242评论 19 139
  • 本文假设读者已经具备 Python 相关的基础知识,并不会介绍如何安装 Python,以及 pip 的使用方法。另...
    import_hello阅读 5,509评论 0 4
  • 你看见天空 有蓝色的冷冽 不行于色的人 贴近液体的火焰 水中绽开妖的妩媚 河水远送灰色的眼神 河边行走的人 茫然得...
    杨昊田阅读 406评论 15 17
  • BGM:Safe and sound 中原中也拉开吧台前的高脚圆凳坐下,大衣折了两折搭在手臂上,和往常一样点了一杯...
    御星辰阅读 906评论 0 0