设计一个文本编辑框,然后将打印的内容导向到图像编辑器上,通过绘图编辑器对文本编辑框的内容进行打印
""" 使用打印机 """ from PyQt5 import QtGui, QtWidgets, QtPrintSupport from PyQt5.QtWidgets import QMainWindow from PyQt5.QtWidgets import * import sys class PrintSupport(QMainWindow): def __init__(self): super(PrintSupport, self).__init__() self.setGeometry(500, 200, 300, 300) self.button = QPushButton("打印QTextEdit控件中的内容", self) self.button.setGeometry(20, 20, 260, 30) self.editor = QTextEdit("默认文本", self) self.editor.setGeometry(20, 60, 260, 200) self.button.clicked.connect(self.print) def print(self): printer = QtPrintSupport.QPrinter() painter = QtGui.QPainter() #将绘制的目标重定向到打印机 painter.begin(printer) #获得文本编辑器的内容 screen = self.editor.grab() #从10,10的位置进行绘制 painter.drawPixmap(10, 10, screen) painter.end() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = PrintSupport() main.show() sys.exit(app.exec_())