Python GUI编程网 > PyQt5高级扩展应用 > QPrintDialog打印对话框
python教程

QPrintDialog打印对话框

发布日期:2021年4月26日 01:02
阅读:1995
作者:Python GUI编程网

QPrintDialog打印对话框

00:00 / 01:23
1x
2x
1.5x
1.25x
1x
0.8x
0.5x

QPrintDialog打印对话框

################################
# PyQt5中文网 - PyQt5全套视频教程 #
#    https://www.PyQt5.cn/     #
#         主讲: 村长            #
################################

from PyQt5.Qt import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QPrintDialog打印对话框 - PyQt5中文网")
        self.resize(600, 500)
        self.func_list()

    def func_list(self):
        self.func()

    def func(self):
        self.printer = QPrinter()

        self.editor = QTextEdit(self)
        self.editor.move(150, 30)
        self.editor.resize(400, 450)
        self.btn1 = QPushButton('打开文件', self)
        self.btn2 = QPushButton('打印设置', self)
        self.btn3 = QPushButton('开始打印', self)
        self.btn1.move(30, 30)
        self.btn2.move(30, 60)
        self.btn3.move(30, 90)

        self.btn1.clicked.connect(self.openfile)
        self.btn2.clicked.connect(self.setdialog)
        self.btn3.clicked.connect(self.showdialog)

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

    def setdialog(self):
        pdialog = QPageSetupDialog(self.printer, self)  # 把打印机放入页面设置对话框
        pdialog.exec()

    def showdialog(self):
        printd = QPrintDialog(self.printer, self)  # 把打印机放入打印对话框
        if QDialog.Accepted == printd.exec():  # 如果对话框选择打印继续执行
            self.editor.print(self.printer)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

    window.show()
    sys.exit(app.exec_())