PyQt5 —— QtDesigner,pyqt5-tools 結(jié)合

Qt 使用可以用 QtDesigner 創(chuàng)建 .ui 文件,并在 QtCreator 中自動(dòng)生成C++文件,從C++中調(diào)用;PyQt 也可以,需要借助 PyQt 提供的 pyuic5 腳本。

工具準(zhǔn)本

  1. QtDesigner
    pip3 install PyQt5-tools
  2. 安裝 PyQt5,包含 pyuic 模塊
    pip3 install PyQt5

正確的步驟

  1. 創(chuàng)建 .ui 文件
    打開 QtDesigner,新建,創(chuàng)建一個(gè)簡(jiǎn)單的界面文件,保存為 foo.ui
image.png
image.png
  1. 轉(zhuǎn)換 .ui 文件為 py 文件
pyuic5 foo.ui -o ui_foo.py
  1. 使用生成的文件
    新建一個(gè) 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)

結(jié)合 pycharm

pycharm 支持 ”external tool" 可以添加自定義的命令到 pycharm 菜單;利用這個(gè),把 pyuic 添加進(jìn)來,提高效率
settings->tools->external tools


image.png

資源文件 qrc

  • 用 QtCreator 或 QtDesigner 編輯 qrc
QtCreator 編輯 qrc

前綴 是 qrc 中用來分組的一個(gè)層級(jí),在源碼中體現(xiàn)為路徑
在前綴下 添加文件:圖片等
語(yǔ)言 是由于國(guó)際化的一個(gè)后綴(不同語(yǔ)言自動(dòng)識(shí)別使用資源)

  • PyQt5 提供工具把 qrc 及其關(guān)聯(lián)的文件編譯為一個(gè) py 文本,只需要引用這個(gè)文本,就可以使用資源。
    pyrcc5.exe image.qrc -o rc_image.py

  • 使用編譯后的模塊
    資源路徑格式 ":前綴/文件名" 或 ":前綴/別名"

資源文件和別名
from rc_image import *
img = QPixmap(":/img/glyphicons-281-settings.png")    # 從編譯后的資源文件中導(dǎo)入(文件名)
img = QPixmap(":/img/settings")    # 從編譯后的資源文件中導(dǎo)入(別名)

demo 地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,290評(píng)論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評(píng)論 19 139
  • 本文假設(shè)讀者已經(jīng)具備 Python 相關(guān)的基礎(chǔ)知識(shí),并不會(huì)介紹如何安裝 Python,以及 pip 的使用方法。另...
    import_hello閱讀 5,561評(píng)論 0 4
  • 你看見天空 有藍(lán)色的冷冽 不行于色的人 貼近液體的火焰 水中綻開妖的嫵媚 河水遠(yuǎn)送灰色的眼神 河邊行走的人 茫然得...
    楊昊田閱讀 421評(píng)論 15 17
  • BGM:Safe and sound 中原中也拉開吧臺(tái)前的高腳圓凳坐下,大衣折了兩折搭在手臂上,和往常一樣點(diǎn)了一杯...
    御星辰閱讀 970評(píng)論 0 0

友情鏈接更多精彩內(nèi)容