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())
相关推荐
大邳草民24 分钟前
深入理解 Python 的属性化方法
开发语言·笔记·python
洗紫2 小时前
Python中的条件语句怎么使用?
python
jf加菲猫2 小时前
第1章 认识Qt
开发语言·c++·qt·ui
南汐汐月2 小时前
重生归来,我要成功 Python 高手--day35 深度学习 Pytorch
pytorch·python·深度学习
java1234_小锋2 小时前
[免费]基于Python的深度学习豆瓣电影数据可视化+情感分析推荐系统(Flask+Vue+LSTM+scrapy)【论文+源码+SQL脚本】
python·信息可视化·flask·电影数据可视化
PieroPc3 小时前
一个基于Python Streamlit sqlite3 的销售单管理系统,提供商品管理、客户管理、销售单管理及打印,和应收对账单等功能
python·oracle·sqlite·streamlit
月下倩影时3 小时前
视觉进阶篇—— PyTorch 安装
人工智能·pytorch·python
Valueyou243 小时前
论文阅读——CenterNet
论文阅读·python·opencv·目标检测·计算机视觉
孤狼warrior3 小时前
目前最新同花顺金融股市数据爬取 JS逆向+node.js补浏览器环境
javascript·爬虫·python·金融·node.js
蒋星熠3 小时前
全栈开发实战指南:从架构设计到部署运维
运维·c++·python·系统架构·node.js·devops·c5全栈