PyQt5-打开文档、保存文档、打印文档

Python 1378 0 2021-01-27

PyQt5-打开文档、保存文档、打印文档
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QTextEdit, QFileDialog, QDialog
from PyQt5.QtGui import QTextDocument
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrintDialog, QPrinter, QPrintPreviewDialog

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

    def initUI(self):

        self.setGeometry(300, 200, 450, 310)
        self.setWindowTitle('关注哈喽吧:学点编程吧--保存、打印文件')


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

        self.bt1 = QPushButton('打开文件',self)
        self.bt1.move(350,20)
        self.bt2 = QPushButton('打开多个文件',self)
        self.bt2.move(350,60)
        self.bt5 = QPushButton('保存文件',self)
        self.bt5.move(350,120)
        self.bt6 = QPushButton('页面设置',self)
        self.bt6.move(350,160)
        self.bt7 = QPushButton('打印预览',self)
        self.bt7.move(350,200)
        self.bt8 = QPushButton('打印文档',self)
        self.bt8.move(350,240)

        self.bt1.clicked.connect(self.openfile)
        self.bt2.clicked.connect(self.openfiles)
        self.bt5.clicked.connect(self.savefile)
        self.bt6.clicked.connect(self.pagesettings)
        self.bt7.clicked.connect(self.printPreview)
        self.bt8.clicked.connect(self.printdialog)

        self.show()
    
    def openfile(self):
        fname = QFileDialog.getOpenFileName(self, '学点编程吧:打开文件','./')
        if fname[0]:
            with open(fname[0], 'r',encoding='utf-8',errors='ignore') as f:
                    self.tx.setText(f.read())
    
    def openfiles(self):
        fnames = QFileDialog.getOpenFileNames(self, '学点编程吧:打开多个文件','./')
        if fnames[0]: 
                for fname in fnames[0]:
                    with open(fname, 'r',encoding='utf-8',errors='ignore') as f:
                        self.tx.append(f.read())
    
    def savefile(self):
        fileName = QFileDialog.getSaveFileName(self, '学点编程吧:保存文件','./',"Text files (*.txt)")
        if fileName[0]:
                with open(fileName[0], 'w',encoding='utf-8',errors='ignore') as f:
                    f.write(self.tx.toPlainText())
    
    def pagesettings(self):
        printsetdialog = QPageSetupDialog(self.printer,self)
        printsetdialog.exec_()

    def printPreview(self):
        previewdialog = QPrintPreviewDialog()
        previewdialog.paintRequested.connect(self.handlePaintRequest)
        previewdialog.exec_()

    def handlePaintRequest(self, printer):
        document = QTextDocument()
        document.setHtml(self.tx.toPlainText())
        document.print(printer)
    
    def printdialog(self):
        printdialog = QPrintDialog(self.printer,self)
        if QDialog.Accepted == printdialog.exec_():
            self.tx.print(self.printer)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

上一篇:PyQt5-QPrinter控件使用

下一篇:没有了

讨论数量:0

请先登录再发表讨论。 2024-04-29

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链