PySide6 打印(QPrinter)文本编辑器(QPlaintextEdit)内容

PySide6 打印(QPrinter)文本编辑器(QPlaintextEdit)内容

不足之处
  • Linux Gnome上测试不稳定
    Windows 10 稳定使用(可能是调用系统自带的打印机)
代码参考

创建基本打印功能

代码示例
bash 复制代码
# coding = utf-8

from PySide6.QtWidgets import QApplication,QWidget,QPushButton,QVBoxLayout,QPlainTextEdit
from PySide6.QtPrintSupport import QPrintDialog,QPrinter
from PySide6.QtGui import QPagedPaintDevice
import sys

class TestPrint(QWidget):

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.setEventBind()

    def setupUi(self):
        self.setWindowTitle("测试打印")

        vbox = QVBoxLayout(self)

        self.plainTextEdit = QPlainTextEdit()
        self.printButton = QPushButton("打印")

        vbox.addWidget(self.plainTextEdit)
        vbox.addWidget(self.printButton)

    def setEventBind(self):
        self.printButton.clicked.connect(self.getPrintDialog)

    def getPrintDialog(self):
        """
        """
        printer = QPrinter()
        dialog = QPrintDialog(printer)
        if dialog.exec():
            self.plainTextEdit.print_(printer)



if __name__ == "__main__":
    app =QApplication(sys.argv)
    testPrint = TestPrint()
    testPrint.show()
    sys.exit(app.exec())
相关推荐
旦莫2 分钟前
Python测试开发工具库:日志脱敏工具(敏感信息自动屏蔽)
python·测试开发·自动化·ai测试
唐叔在学习7 分钟前
Python自动化指令进阶:UAC提权
后端·python
旺仔小拳头..8 分钟前
Java ---变量、常量、类型转换、默认值、重载、标识符、输入输出、访问修饰符、泛型、迭代器
java·开发语言·python
wujj_whut39 分钟前
【Conda实战】从0到1:虚拟环境创建、多Python版本管理与环境切换全指南
开发语言·python·conda
geoqiye43 分钟前
2026官方认证:贵阳宠物行业短视频运营TOP5评测
大数据·python·宠物
龙腾AI白云1 小时前
AI智能体搭建(3)深度搜索智能体如何搭建与设计 Agent#智能体搭建#多智能体#VLA#大模型
python·django·virtualenv·scikit-learn·tornado
海棠AI实验室1 小时前
第十一章 错误处理体系:异常分层与可恢复策略
python·异常处理
love530love1 小时前
EPGF 新手教程 22教学模板不是压缩包:EPGF 如何设计“可复制、可检查、可回收”的课程模板?
ide·人工智能·windows·python·架构·pycharm·epgf
ai_top_trends2 小时前
不同 AI 生成 2026 年工作计划 PPT 的使用门槛对比
人工智能·python·powerpoint
adayabetter2 小时前
Python自动化办公提效相关脚本
python·自动化·自动化脚本