打印图像-自写打印方法
1 import sys 2 from PyQt5.QtCore import Qt, QPoint, QRect 3 from PyQt5.QtGui import QPainter, QPixmap, QIcon 4 from PyQt5.QtPrintSupport import QPrintDialog, QPrinter 5 from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QVBoxLayout 6 7 class Demo(QWidget): 8 def __init__(self): 9 super(Demo, self).__init__() 10 self.resize(600, 600) 11 12 self.begin_point = QPoint() 13 self.end_point = QPoint() 14 15 self.pix = QPixmap(600, 600) 16 self.pix.fill(Qt.white) 17 18 self.printer = QPrinter() #实例化打印对象 19 20 self.print_btn = QPushButton(self) 21 self.print_btn.setIcon(QIcon(r'C:UsersmingDesktop2.png')) 22 self.print_btn.clicked.connect(self.open_printer_func) 23 #点击打印按钮进入打印设置函数 24 25 self.h_layout = QHBoxLayout() 26 self.v_layout = QVBoxLayout() 27 self.h_layout.addWidget(self.print_btn) 28 self.h_layout.addStretch(1) 29 self.v_layout.addLayout(self.h_layout) 30 self.v_layout.addStretch(1) 31 32 self.setLayout(self.v_layout) 33 34 def open_printer_func(self):#打印设置函数 35 printer_dialog = QPrintDialog(self.printer) #实例化一个打印设置对话框。 36 #参数 打印对象 37 if printer_dialog.exec_(): #以模态方式打开 打印设置对话框 38 painter = QPainter(self.printer) #把打印对象作为画布 39 painter.drawPixmap(0, 0, self.pix)#把需要打印的图像添加到画布[也是添加到打印对象上] 40 41 def paintEvent(self, QPaintEvent): 42 painter = QPainter(self) 43 painter.drawPixmap(0, 0, self.pix) 44 45 if self.begin_point and self.end_point: 46 rect = QRect(self.begin_point, self.end_point) 47 painter.drawRect(rect) 48 49 def mousePressEvent(self, QMouseEvent): 50 if QMouseEvent.button() == Qt.LeftButton: 51 self.begin_point = QMouseEvent.pos() 52 self.end_point = self.begin_point 53 self.update() 54 55 def mouseMoveEvent(self, QMouseEvent): 56 if QMouseEvent.buttons() == Qt.LeftButton: 57 self.end_point = QMouseEvent.pos() 58 self.update() 59 60 def mouseReleaseEvent(self, QMouseEvent): 61 if QMouseEvent.button() == Qt.LeftButton: 62 painter = QPainter(self.pix) 63 rect = QRect(self.begin_point, self.end_point) 64 painter.drawRect(rect) 65 self.begin_point = self.end_point = QPoint() 66 self.update() 67 68 69 if __name__ == '__main__': 70 app = QApplication(sys.argv) 71 demo = Demo() 72 demo.show() 73 sys.exit(app.exec_())
打印文本-控件自带打印方法
1 import sys 2 from PyQt5.QtPrintSupport import QPrintDialog, QPrinter 3 from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit, QVBoxLayout 4 5 class Demo(QWidget): 6 def __init__(self): 7 super(Demo, self).__init__() 8 self.text_edit = QTextEdit(self) 9 self.print_btn = QPushButton('打印', self) 10 self.print_btn.clicked.connect(self.open_printer_func) 11 12 self.printer = QPrinter() #实例化一个打印对象 13 14 self.v_layout = QVBoxLayout() 15 self.v_layout.addWidget(self.text_edit) 16 self.v_layout.addWidget(self.print_btn) 17 self.setLayout(self.v_layout) 18 19 def open_printer_func(self): 20 printer_dialog = QPrintDialog(self.printer) #实例化一个打印设置对话框 21 # 参数 打印对象 22 if printer_dialog.exec_():#以模态方式打开 打印设置对话框 23 self.text_edit.print(self.printer)#把打印对象传给控件的print方法 24 25 if __name__ == '__main__': 26 app = QApplication(sys.argv) 27 demo = Demo() 28 demo.show() 29 sys.exit(app.exec_())