pyqt출력에 해당하는 글 1

[PYQT5] 프린트 할 위젯 선택하고 프린트 하는 예제

Computer 비관심/PyQt5|2020. 7. 18. 18:30
반응형

간단한 예제로 어떻게 작동하는지 응용하기 쉬운 소스입니다.

 

QPrinter 클레스를 사용해서 출력합니다.

import sys

from PyQt5 import QtGui, QtWidgets, QtPrintSupport


class App(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        # Create some widgets
        self.setGeometry(500, 500, 300, 300)
        self.button = QtWidgets.QPushButton(
            'Print QTextEdit widget (the one below)', self)
        self.button.setGeometry(20, 20, 260, 30)
        self.editor = QtWidgets.QTextEdit(
            'Wow such text why not change me?', self)
        self.editor.setGeometry(20, 60, 260, 200)
        self.button.clicked.connect(self.print_widget)

    def print_widget(self):
        # Create printer
        printer = QtPrintSupport.QPrinter()
        # Create painter
        painter = QtGui.QPainter()
        # Start painter
        painter.begin(printer)
        # Grab a widget you want to print
        screen = self.editor.grab()
        # Draw grabbed pixmap
        painter.drawPixmap(10, 10, screen)
        # End painting
        painter.end()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    gui = App()
    gui.show()
    app.exec_()

 

만약 전체를 출력하고 싶다면 screen =self.editor.grab() 대신 screen = self.grab()을 하면 됩니다.

 

출처: https://stackoverflow.com/questions/42455904/printing-from-main-page-in-pyqt5

댓글()