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

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

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


视频链接

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


课程重点

在这里插入图片描述


代码位置

在这里插入图片描述


完整代码

'''

显示打印对话框

'''
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模式)

# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.csdn.net/zzx188891020
@E-mail: 188891020@qq.com
@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_())


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值