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())
相关推荐
Shi_haoliu5 分钟前
python安装操作流程-FastAPI + PostgreSQL简单流程
python·postgresql·fastapi
ZH154558913114 分钟前
Flutter for OpenHarmony Python学习助手实战:API接口开发的实现
python·学习·flutter
小宋102116 分钟前
Java 项目结构 vs Python 项目结构:如何快速搭一个可跑项目
java·开发语言·python
Mongnewer40 分钟前
试写UI界面设计器
ui·界面设计器
一晌小贪欢1 小时前
Python 爬虫进阶:如何利用反射机制破解常见反爬策略
开发语言·爬虫·python·python爬虫·数据爬虫·爬虫python
躺平大鹅1 小时前
5个实用Python小脚本,新手也能轻松实现(附完整代码)
python
yukai080081 小时前
【最后203篇系列】039 JWT使用
python
独好紫罗兰2 小时前
对python的再认识-基于数据结构进行-a006-元组-拓展
开发语言·数据结构·python
Dfreedom.2 小时前
图像直方图完全解析:从原理到实战应用
图像处理·python·opencv·直方图·直方图均衡化