标签:request ext 图片 exec 直接 label rtt elf 分享
1 from PyQt5.QtGui import QFont,QTextDocument,QTextCursor 2 from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QSizePolicy, QAction,QDialog 3 from PyQt5.QtPrintSupport import QPrinter, QPrintDialog,QPrintPreviewDialog 4 import sys 5 6 ################################################ 7 #######打印文本---海子的诗 8 ################################################ 9 the_text = ‘‘‘ 10 从明天起,做一个幸福的人 11 喂马、劈柴,周游世界 12 从明天起,关心粮食和蔬菜 13 我有一所房子,面朝大海,春暖花开 14 从明天起,和每一个亲人通信 15 告诉他们我的幸福 16 那幸福的闪电告诉我的 17 我将告诉每一个人 18 给每一条河每一座山取一个温暖的名字 19 陌生人,我也为你祝福 20 愿你有一个灿烂的前程 21 愿你有情人终成眷属 22 愿你在尘世获得幸福 23 我只愿面朝大海,春暖花开 24 ‘‘‘ 25 26 class MainWindow(QMainWindow): 27 def __init__(self, parent=None): 28 super(MainWindow, self).__init__(parent) 29 self.setWindowTitle(self.tr("打印功能")) 30 31 # 创建文本框 32 self.label = QLabel() 33 self.label.setFont(QFont("宋体",12,QFont.Bold)) 34 self.label.setText(the_text) 35 self.setCentralWidget(self.label) 36 37 # 创建菜单栏 38 self.createMenus() 39 40 def createMenus(self): 41 # 创建动作一 42 self.printAction1 = QAction(self.tr("打印无预留"), self) 43 self.printAction1.triggered.connect(self.on_printAction1_triggered) 44 45 # 创建动作二 46 self.printAction2 = QAction(self.tr("打印有预留"), self) 47 self.printAction2.triggered.connect(self.on_printAction2_triggered) 48 49 # 创建动作三 50 self.printAction3 = QAction(self.tr("直接打印"), self) 51 self.printAction3.triggered.connect(self.on_printAction3_triggered) 52 53 # 创建动作四 54 self.printAction4 = QAction(self.tr("打印到PDF"), self) 55 self.printAction4.triggered.connect(self.on_printAction4_triggered) 56 57 58 # 创建菜单,添加动作 59 self.printMenu = self.menuBar().addMenu(self.tr("打印")) 60 self.printMenu.addAction(self.printAction1) 61 self.printMenu.addAction(self.printAction2) 62 self.printMenu.addAction(self.printAction3) 63 self.printMenu.addAction(self.printAction4) 64 65 66 67 # 动作一:打印,无预览 68 def on_printAction1_triggered(self): 69 printer = QPrinter() 70 printDialog = QPrintDialog(printer, self) 71 if printDialog.exec_() == QDialog.Accepted: 72 self.handlePaintRequest(printer) 73 74 75 # 动作二:打印,有预览 76 def on_printAction2_triggered(self): 77 dialog = QPrintPreviewDialog() 78 dialog.paintRequested.connect(self.handlePaintRequest) 79 dialog.exec_() 80 81 # 动作三:直接打印 82 def on_printAction3_triggered(self): 83 printer = QPrinter() 84 self.handlePaintRequest(printer) 85 86 # 动作四:打印到pdf 87 def on_printAction4_triggered(self): 88 printer = QPrinter() 89 printer.setOutputFormat(QPrinter.PdfFormat) 90 printer.setOutputFileName("D:/pdf打印测试.pdf") 91 self.handlePaintRequest(printer) 92 93 94 ## 打印函数 95 def handlePaintRequest(self, printer): 96 document = QTextDocument() 97 cursor = QTextCursor(document) 98 cursor.insertText(self.label.text()) 99 document.print(printer) 100 101 102 if __name__ == "__main__": 103 app = QApplication(sys.argv) 104 main = MainWindow() 105 main.show() 106 sys.exit(app.exec_())
标签:request ext 图片 exec 直接 label rtt elf 分享
原文地址:https://www.cnblogs.com/XJT2018/p/9871730.html