pyside6学习笔记

pyside6 是什么

我理解它是一个能够用python调用的Qt软件包系列,安装完成后会包含了很多功能套装,程序包有:pyside6-designer、pyside6-rcc、pyside6-qmlls、pyside6-qml、pyside6-lupdate、pyside6-uic、pyside6-lrelease和pyside6-linguist等工具套装。例如uic的功能就是把ui界面文件(xml格式)转换到python文件。许多软件有类似的思路,例如grpc
pyqt5
pyqt6 不支持win7/8

pyside6 (数字6代表Qt6)
pyside经历了pyside2到目前的pyside6,在资料和组件支持上和pyqt6现在差不多,最大的差别在于它们的许可协议上,而PySide6许可协议更友好,也是Qt官方公司主推的框架

pyside6 怎么安装

linux上安装pyside6

source /home/wangmin/py3env/bin/activate
pip3 install pyside6 -i https://pypi.doubanio.com/simple 1

vscode上怎么使用Qt

extensions 安装Qt for Python
参考文档
https://github.com/seanwu1105/vscode-qt-for-python#predefined-variables

官方教程:3

pycharm怎么安装:
可以参考教程 https://www.eolink.com/news/post/13905.html

界面程序要点

界面到代码的转变

其实界面文件最终是要反馈到py代码文件中
ui文件(XML文件)可以通过uic工具转换到py文件,或直接引用
一、使用ui文件的两种方式:

  • 直接用文件
from PySide6.QtUiTools import QUiLoader
self.ui = QUiLoader().load("文件名")

  • ui文件生成对应py
    例如 a.ui 生成 ui_a.py

所以本教程只以写代码方式进行布局,方便自我学习。

信号-槽 机制

基本概念
信号:signal
槽函数:slot
关联信号槽:connect
https://zhuanlan.zhihu.com/p/603617075

有两种方式加信号和槽:

  • 通过代码
  • 通过界面设计器

内部原理 参考5
Qt的moc机制
回调函数的例子

布局

布局类中文解释
QHBoxLayout线性水平布局
QVBoxLayout线性垂直布局
QGridLayout网格布局
QStackedLayout堆叠布局

菜单条 学习网站6
嵌套布局 学习网站
https://www.pythonguis.com/tutorials/pyside6-layouts/

参考4
加入布局版 qcaculator git tag为v1.0.2

打包发布

待补充

qss

待补充

qml

待补充

项目实践

简单计算器 demo

包含基本功能:

  • 计算表达式子
  • 表达式历史记录一下

知识点:
pyside6组件,信号槽

from PySide6.QtWidgets import QMainWindow,QTextEdit,QTextBrowser,QPushButton,QApplication
from PySide6.QtGui import QTextCursor
class MainWindow:
    def __init__(self) -> None:
        self.main_window = QMainWindow()
        self.main_window.resize(500,400)
        self.main_window.move(50,50)
        self.main_window.setWindowTitle("简单计算demo")
        
        self.caculator_text = QTextEdit(self.main_window)
        self.caculator_text.move(50,50)
        self.caculator_text.resize(200,100)
        self.caculator_text.setPlaceholderText("在此输入计算表达式")

        self.calc_button = QPushButton("计算",self.main_window)
        self.calc_button.move(260, 50)
        self.calc_button.resize(100,50)
        self.calc_button.clicked.connect(self.calc_result)

        self.result_text = QTextBrowser(self.main_window)
        self.result_text.move(50, 150)
        self.result_text.resize(300,300)

    def calc_result(self):
        try:
            text = f"{self.caculator_text.toPlainText()} = {eval(self.caculator_text.toPlainText())}"
        except Exception as e:
            text = f"错误表达式:{self.caculator_text.toPlainText()},错误码为:{str(e)}"
        self.result_text.insertPlainText(text)
        self.result_text.moveCursor(QTextCursor.MoveOperation.End)
        self.result_text.insertPlainText("\n")
        self.caculator_text.clear()
        print(text)

if __name__ == "__main__":
    app = QApplication()
    calc_win = MainWindow()
    calc_win.main_window.show()
    app.exec()

demo运行情况:
在这里插入图片描述

markdown编辑器 demo

包含基本功能:

  • 打开/保存md文件
  • 回退操作
  • 预览md文件

知识点:
pyside6组件,信号槽,布局

from PySide6.QtWidgets import QApplication,QMainWindow,QTextEdit,\
    QTextBrowser,QVBoxLayout,QMenuBar,QMenu,QHBoxLayout,QWidget,QFileDialog
from PySide6.QtGui import QIcon, QAction
class MainWindow(QMainWindow):
    def __init__(self) -> None:
        self.main_window = QMainWindow()
        self.main_window.resize(1000,600)
        self.main_window.setWindowTitle("markdown编辑器")
        #self.action = QAction(MainWindow)
        #self.action.setObjectName(u"action")

        self.file_path = ""
        
        self.menu_bar = QMenuBar(self.main_window)

        self.menu = QMenu(self.menu_bar)
        self.menu_bar.addMenu(self.menu)
        self.menu.setTitle("功能列表")
        
        self.open_file = QAction(QIcon(),"打开文件",self.menu_bar)
        self.open_file.triggered.connect(self.open_file_func)
        self.menu.addAction(self.open_file)
        

        self.save_file = QAction(QIcon(),"保存文件",self.menu_bar)
        self.menu.addAction(self.save_file)
        self.save_file.triggered.connect(self.file_saved_func)
        

        self.save_as_file = QAction(QIcon(),"另存文件",self.menu_bar)
        self.menu.addAction(self.save_as_file)
        
        

        
        self.huitui = QAction(QIcon('img/houtui.jpg'),"&回退", self.menu_bar)
        self.huitui.setObjectName("回退")
        self.huitui.setShortcut("Ctrl+h")
        self.huitui.setStatusTip("操作文档回退")
    

        self.menu_bar.addAction(self.huitui)


        self.qianjin = QAction(QIcon('img/qianjin.jpg'),"&前进", self.menu_bar)
        self.qianjin.setObjectName("qianjin")
        self.qianjin.setShortcut("Ctrl+q")
        self.qianjin.setStatusTip("操作文档前进")


        self.menu_bar.addAction(self.qianjin)

        self.about = QAction(QIcon(''),"&关于", self.menu_bar)
        self.about.setObjectName("关于")
        self.about.setStatusTip("关于")
        self.menu_bar.addAction(self.about)

        self.layout = QHBoxLayout(self.main_window)
        self.layout.setMenuBar(self.menu_bar)
        
        
        self.content_layout =QHBoxLayout()
        self.markdown_context = QTextEdit(self.main_window)
        self.markdown_browser = QTextBrowser(self.main_window)
        self.content_layout.addWidget(self.markdown_context)
        self.content_layout.addWidget(self.markdown_browser)

        self.huitui.triggered.connect(self.markdown_context.redo)
        self.qianjin.triggered.connect(self.markdown_context.undo)
        self.markdown_context.textChanged.connect(self.file_changed_func)
        

        self.layout.addLayout(self.content_layout)
        
        # self.main_window.setLayout(self.layout)
        # self.main_window.setCentralWidget(self.layout)

        widget = QWidget()
        widget.setLayout(self.layout)
        self.main_window.setCentralWidget(widget)        

        
        #self.main_window.setLayout(self.layout)  

    def open_file_func(self):
        self.file_path,_ = QFileDialog.getOpenFileName(self.main_window,"选择要编辑的md文件","","图片类型 (*.md)")
        with open(self.file_path,"r") as f:
            content = f.read()
            self.markdown_context.setText(content)
            self.markdown_browser.setMarkdown(content)
    
    
    def file_changed_func(self):
        self.markdown_browser.setMarkdown(self.markdown_context.toPlainText())   
        
    def file_saved_func(self):
        new_file_path,_= QFileDialog.getSaveFileName(self.main_window,"保存md文件")
        print(new_file_path)
        with open(new_file_path,"w") as f:
            f.write(self.markdown_context.toPlainText())

    def houtui_func(self):
        pass
        


if __name__ == "__main__":
   app = QApplication()
   win = MainWindow()
   win.main_window.show()
   app.exec()

demo效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值