python qt6学习
利用designer创建窗口,生成ui
designer在python的位置:
C:\Users\bu\AppData\Local\Programs\Python\Python39\Scripts\pyside6-designer.exe
从 a.ui 文件生成 Python 文件:pyside6-uic -i form.ui -o ui_form.py
从 a.qrc 文件生成 Python 文件:pyside6-rcc -i resources.qrc -o rc_resources.py
使用 pyside6-designer 命令打开 Qt Designer 以编辑或创建 .ui 文件
使用 pyside6-uic 工具可以将这些 .ui 文件转换为 Python 代码,让你可以在主函数中引用这些代码。所以在你部署的应用程序中包含 .ui 文件并不是必需的。
简单例子
import sys from PySide6.QtWidgets import QApplication, QLabel app = QApplication(sys.argv) label = QLabel("Hello World!") label.show() app.exec_()
可以向 QApplication 对象传递任意参数。一般情况下我们不需要传递参数,或者也可以像下面这样写:
还创建了 QLabel 对象。QLabel 是一个可以显示文本和图像的容器。文本可以是简单文本,也可以是富文本,像HTML一样:
label = QLabel("<font color=red size=40>Hello World!</font>")
在创建完一个标签后,我们要对它调用 show() 函数。
调用 app.exec_() 进入主循环,开始执行代码
简单的按钮
import sys from PySide6.QtWidgets import QApplication, QPushButton from PySide6.QtCore import Slot def say_hello(): print("Button clicked, Hello!") app = QApplication(sys.argv) # 在创建时传入的字符串会显示在按钮上 button = QPushButton("Click me") #button.clicked,执行say_hello() button.clicked.connect(say_hello()) #显示这个按钮,并进入主循环 button.show() app.exec_()
使用QUiLoader和pyside6-uic导入ui文件
Qt Designer 的使用在官网的 Using Qt Designer(https://doc.qt.io/qtcreator/creator-using-qt-designer.html) 教程里详细介绍。
方法一:生成 Python 类
实例:
import sys from PySide6.QtWidgets import QApplication, QMainWindow from PySide6.QtCore import QFile from ui_mainwindow import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
方法二:直接加载 UI 文件
QUiLoader 类可以让你动态地载入 UI 文件:
# File: main.py import sys from PySide6.QtUiTools import QUiLoader from PySide6.QtWidgets import QApplication from PySide6.QtCore import QFile, QIODevice if __name__ == "__main__": app = QApplication(sys.argv) ui_file_name = "mainwindow.ui" ui_file = QFile(ui_file_name) if not ui_file.open(QIODevice.ReadOnly): print(f"Cannot open {ui_file_name}: {ui_file.errorString()}") sys.exit(-1) loader = QUiLoader() window = loader.load(ui_file) ui_file.close() if not window: print(loader.errorString()) sys.exit(-1) window.show() sys.exit(app.exec())
常用属性
.appendPlainText("添加文本\n") #插入文本
.clear() #清空文本内容
.toPlainText() 复制文本框的内容
.setPlainText() 设置
消息框
一、提供的类型
QMessageBox.information 信息框
QMessageBox.question 问答框
QMessageBox.warning 警告
QMessageBox.ctitical危险
QMessageBox.about 关于
二、引用
from PyQt6.QtWidgets import QMessageBox
三、代码示例
复制代码
#self 当前窗口的夫窗口 # 消息:信息 QMessageBox.information(self,"消息框标题","这是一条消息。",QMessageBox.Yes | QMessageBox.No) # 消息:问答 QMessageBox.question(self,"消息框标题","这是一条问答。",QMessageBox.Yes | QMessageBox.No) # 消息:警告warning QMessageBox.warning(self,"消息框标题","这是一条警告。",QMessageBox.Yes | QMessageBox.No) # 消息:危险ctitical QMessageBox.ctitical(self,"消息框标题","这是一条危险。",QMessageBox.Yes | QMessageBox.No) # 消息:关于 QMessageBox.about(self,"消息框标题","这是关于软件的说明",QMessageBox.Yes | QMessageBox.No)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库