【QTdesigner】课时63.显示打印对话框(QPrinter)(QPrintDialog)(QPageSetupDialog)【pyqt5+QTdesigner模式】

课程目录

  • 视频链接
  • 课程重点
  • 代码位置
  • 完整代码
  • 遇到的问题
  • 改写效果展示
  • 改写注意点
  • 改写代码(QTdesigner模式)

>>> 点击进入:pyqt5专栏<<<

老师原课件下载地址:
有积分的朋友,支持下我,打赏也OK。
不下载也可以,我每节课会吧代码贴出来


视频链接

P62课时63.显示打印对话框


课程重点

【QTdesigner】课时63.显示打印对话框(QPrinter)(QPrintDialog)(QPageSetupDialog)【pyqt5+QTdesigner模式】_第1张图片


代码位置

在这里插入图片描述


完整代码

'''

显示打印对话框

'''
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QTextEdit, QFileDialog, QDialog
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrintDialog, QPrinter
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.showSettingsDialog)
        self.printButton.clicked.connect(self.showPrintDialog)

    # 打开文件
    def openFile(self):
        fname = QFileDialog.getOpenFileName(self,'打开文本文件','./')
        if fname[0]:
            with open(fname[0],'r',encoding='utf-8',errors = 'ignore') as f:
                self.editor.setText(f.read())
    # 显示打印设置对话框
    def showSettingsDialog(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 = QApplication(sys.argv)
    gui = PrintDialog()
    gui.show()
    sys.exit(app.exec_())

遇到的问题

必须定义QPrinter()


改写效果展示

【QTdesigner】课时63.显示打印对话框(QPrinter)(QPrintDialog)(QPageSetupDialog)【pyqt5+QTdesigner模式】_第2张图片


改写注意点

【QTdesigner】课时63.显示打印对话框(QPrinter)(QPrintDialog)(QPageSetupDialog)【pyqt5+QTdesigner模式】_第3张图片


改写代码(QTdesigner模式)

# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.csdn.net/zzx188891020
@E-mail: [email protected]
@File: class63.py
@CreateTime: 2020/5/29 8:03
'''

import sys
from PyQt5.QtWidgets import QApplication,QWidget,QFileDialog,QDialog
from PyQt5 import uic
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrintDialog,QPrinter


class my_form(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi('../ui_package/class63.ui', self)
        self.printer = QPrinter()
        self.openButton.clicked.connect(self.openFile)
        self.settingsButton.clicked.connect(self.showSettingsDialog)
        self.printButton.clicked.connect(self.showPrintDialog)



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

    # 显示打印设置对话框
    def showSettingsDialog(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 = QApplication(sys.argv)
    main = my_form()
    main.show()
    sys.exit(app.exec_())


你可能感兴趣的:(pyqt5)