PyQt5基础学习-QPrintDialog(打印机对话框) 1.QPageSetupDialog(打印机设置对话框) 2.QPageSetupDialog().exec(打印机对话框执行) 3.QTextEdit().print(QPrinter())(文本框打印内容)

通过点击按钮, 打开文件, 打开打印机设置, 打开打印机对话界面进行打印

复制代码
"""
显示打印对话框
"""

from PyQt5 import QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import *
from PyQt5.QtPrintSupport import *
import sys

class PrintDialog(QWidget):
    def __init__(self):
        super(PrintDialog, self).__init__()
        self.printer = QPrinter()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 400)
        self.setWindowTitle("打印对话框")

        self.editor = QTextEdit(self)
        self.editor.setGeometry(20, 20, 300, 270)

        self.openButton = QPushButton("打开文件", self)
        self.openButton.move(350, 20)

        self.settingsButton = QPushButton("打印设置", self)
        self.settingsButton.move(350, 50)

        self.printButton = QPushButton("打印文档", self)
        self.printButton.move(350, 80)

        self.openButton.clicked.connect(self.openFile)
        self.settingsButton.clicked.connect(self.showSettingDialog)
        self.printButton.clicked.connect(self.showPrintDialog)

    #打开文件
    def openFile(self):

        fname = QFileDialog.getOpenFileName(self, "打开文件", "./")
        if fname[0]:
            with open(fname[0], 'r', encoding='utf-8') as f:
                self.editor.setText(f.read())

    #显示打印设置对话框
    def showSettingDialog(self):
        printDialog = QPageSetupDialog(self.printer, self)
        printDialog.exec()

    #显示打印对话框
    def showPrintDialog(self):
        printdialog = QPrintDialog(self.printer, self)
        if QDialog.Accepted == printdialog.exec():
            self.editor.print(self.printer)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main = PrintDialog()
    main.show()

    sys.exit(app.exec_())
复制代码

 

posted @   c语言我的最爱  阅读(661)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示