PyQt5 프린트 할 위젯 선택하고 프린트 하는 예제
- Link
- https://stackoverflow.com/questions/42455904/printing-from-main-page-in-pyqt58
- https://infocentre.tistory.com/category/Computer%20%EB%B9%84%EA%B4%80%EC%8B%AC/PyQt57
레퍼런스: https://doc.bccnsoft.com/docs/PyQt5/
위키Doc : https://wikidocs.net/book/2165
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()을 하면 됩니다.
[이 게시물은 관리자 님에 의해 2023-10-11 15:22:14 파이썬 Python 에서 이동됨]