Pyside6 学习笔记

002 环境搭建

  1. pyside6本体安装
pip install pyside6

全局安装的情况下会自动安装qt designer

  1. Zeal

文档查阅工具:https://zealdocs.org/

查找Qt6的文档下载

  1. qtdesigner

qt designer能点开能拖动就没问题了

  1. vscode插件

下载一个插件 PYQT integration

扩展设置:pyrcc pyuic Qtdesigner的Path需要设置

右键可以打开designer

003 基础框架

from PySide6.QtWidgets import QApplication, QWidget
from PySide6.QtCore import Qt

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        
if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

空窗口

004 三个最基础的控件

在Qt Designer中可以看到控件的属性

QPushButton

from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from PySide6.QtCore import Qt

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        btn = QPushButton('按钮123', self)
        btn.setGeometry(0, 0, 200, 100)
        btn.setToolTip('ToolTip测试')
        btn.setText('重新设置')
            
if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

QLabel

from PySide6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PySide6.QtCore import Qt

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        mainLayout = QVBoxLayout()
        lb = QLabel('我是一个标签', self)
        lb.setText('修改的文字')
        lb.setAlignment(Qt.AlignmentFlag.AlignHCenter)
        mainLayout.addWidget(lb)
        self.setLayout(mainLayout)
        
if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

QLineEdit

        super().__init__()
        mainLayout = QVBoxLayout()
        
        line = QLineEdit()
        line.setPlaceholderText("请输入内容")
        
        mainLayout.addWidget(line)
        self.setLayout(mainLayout)
        line = QLineEdit(self)
        line.setPlaceholderText("请输入内容")

005 QtDesigner 拖拉控件

登录 计算器

006 QtDesigner

简单介绍QtDesigner

单选按钮 多选按钮

以view(model-based)结尾的需要配合代码展示

Ctrl+R快捷启动展示

container:group box、tab widget

input widget 很重要 快捷键:key sequence edit

  • 界面转换
// 将ui文件转换为python文件
pyside6-uic xxx.ui -o xxx.py
  • 使用静态编译的文件
from PySide6.QtWidgets import QApplication, QWidget
from login import Ui_Form

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        
if __name__=='__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

# 利用多继承的特性
from PySide6.QtWidgets import QApplication, QWidget
from login import Ui_Form

class MyWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        
        self.setupUi(self)
        
if __name__=='__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

007 信号与槽

别的图形界面里面可能叫“事件”“事件响应”

signal & slot

slot一般是函数def hello之类的

from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from PySide6.QtCore import Qt

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        mainlayout = QVBoxLayout()
        # 按钮
        button = QPushButton('按钮123')
        button.setGeometry(0, 0, 200, 100)
        button.setToolTip('ToolTip测试')
        button.clicked.connect(self.hello)
        # 添加到布局中
        mainlayout.addWidget(button)
        self.setLayout(mainlayout)
    
    def hello(self):
        print("hello world")


if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()
from PySide6.QtWidgets import QApplication, QWidget
from login import Ui_Form

class MyWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        # 设置ui
        self.setupUi(self)
        self.pushButton.clicked.connect(self.loginFunc)
    
    def loginFunc(self):
        # 拿到账号
        account = self.lineEdit.text()
        # 拿到密码
        password = self.lineEdit_2.text()
        if account == '123' and password == '123':
            print("successs")
        else:
            print("failure")
        
if __name__=='__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

008 练手:计算器实现

eval()函数:自动识别字符串

from PySide6.QtWidgets import QApplication, QWidget
from Ui_cal import Ui_Form

class MyWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        # 设置ui
        self.setupUi(self)
        
        self.result = ''
        
        self.bind()
        
    def bind(self):
        self.pushButton_0.clicked.connect(lambda: self.addNumber('0'))
        self.pushButton_1.clicked.connect(lambda: self.addNumber('1'))
        self.pushButton_2.clicked.connect(lambda: self.addNumber('2'))
        self.pushButton_3.clicked.connect(lambda: self.addNumber('3'))
        self.pushButton_4.clicked.connect(lambda: self.addNumber('4'))
        self.pushButton_5.clicked.connect(lambda: self.addNumber('5'))
        self.pushButton_6.clicked.connect(lambda: self.addNumber('6'))
        self.pushButton_7.clicked.connect(lambda: self.addNumber('7'))
        self.pushButton_8.clicked.connect(lambda: self.addNumber('8'))
        self.pushButton_9.clicked.connect(lambda: self.addNumber('9'))
        self.pushButton_plus.clicked.connect(lambda: self.addNumber('+'))
        self.pushButton_minus.clicked.connect(lambda: self.addNumber('-'))
        self.pushButton_x.clicked.connect(lambda: self.addNumber('*'))
        self.pushButton_division.clicked.connect(lambda: self.addNumber('/'))
        self.pushButton_dot.clicked.connect(lambda: self.addNumber('.'))
        self.pushButton_cal.clicked.connect(self.equal)
    
    def addNumber(self, number):
        print(number)
        self.lineEdit.clear()
        self.result += number
        self.lineEdit.setText(self.result)
    
    def equal(self):
        self.numberResult = eval(self.result)
        self.lineEdit.setText(str(self.numberResult))
    
        
if __name__=='__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

009 常用控件:QComboBox

可以在QtDesigner里面设置一些参数

from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox
from PySide6.QtCore import Qt

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        # 控件
        cb = QComboBox()
        cb.addItems(['a', 'b', 'c'])
        cb.currentTextChanged.connect(self.showName)
        
        # 添加到布局中
        mainlayout = QVBoxLayout()
        mainlayout.addWidget(cb)
        self.setLayout(mainlayout)
    
    def showName(self, name):
        print(name)


if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

010 常用控件:QCheckBox

from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QCheckBox, QPushButton
from PySide6.QtCore import Qt

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        # 控件
        cb = QCheckBox('是否被选中')
        cb.stateChanged.connect(self.showState)
        
        btn = QPushButton('获取状态')
        btn.clicked.connect(lambda: print(cb.isChecked()))
        
        # 添加到布局中
        mainlayout = QVBoxLayout()
        mainlayout.addWidget(cb)
        mainlayout.addWidget(btn)
        self.setLayout(mainlayout)
    
    def showState(self, state):
        print(state)


if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

011/012 练手:进制转换器

from PySide6.QtWidgets import QApplication, QWidget
from Ui_trans import Ui_Form

class MyWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        
        # 用于存储数据类型的字典
        self.lengthVar = {'米':100, '千米': 100000, '厘米':1, '分米':10}
        self.weightVar = {'克':1, '千克':1000, '斤':500}
        
        self.TypeDict = {'长度': self.lengthVar, '质量': self.weightVar}
        
        self.dataType.addItems(self.TypeDict.keys())
        self.comboBox_1.addItems(self.lengthVar.keys())
        self.comboBox_2.addItems(self.lengthVar.keys())
        self.bind()
    
    def bind(self):
        self.dataType.currentTextChanged.connect(self.typeChanged)
        self.pushButton.clicked.connect(self.calc)
    
    def calc(self):
        
        bigType = self.dataType.currentText()
        # 获取第一个输入框的值
        value = self.input1.text()
        if value == '':
            return
        
        currentType = self.comboBox_1.currentText()
        transType = self.comboBox_2.currentText()
        # 浮点数 标准化(统一单位)
        standardization = float(value) * self.TypeDict[bigType][currentType]
        result = standardization / self.TypeDict[bigType][transType]
        
        self.originData.setText(f'{value} {currentType} =')
        self.transData.setText(f'{result} {transType}')
        self.input2.setText(str(result))
        
    def typeChanged(self, text):
        self.comboBox_1.clear()
        self.comboBox_2.clear()
        
        self.comboBox_1.addItems(self.TypeDict[text].keys())
        self.comboBox_2.addItems(self.TypeDict[text].keys())
        
    
if __name__=='__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

013 常用控件:QRadioButton【单选框】

属性:

checkable 是否能被选择

checked 是否选择

014 常用控件:QTextEdit和QPlainTextEdit

QTextEdit 富文本输入框

QPlainTextEdit 纯文本输入框

常用slots:

  • append 添加文本
  • clear 清空
  • setText 设置文本 / setPlainText 设置纯文本
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QPlainTextEdit

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        textEdit = QTextEdit()
        textEdit.setText("title")
        plainTextEdit = QPlainTextEdit()
        plainTextEdit.setPlainText('content')
        plainTextEdit.appendPlainText('content2')
        
        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(textEdit)
        self.mainLayout.addWidget(plainTextEdit)
        self.setLayout(self.mainLayout)
        
        
if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()