In this Python PySide6 article we are going to learn How to Create Print Preview in Python PySide6, Printing is common task in many applications and providing print preview is often necessary to give users an idea of what the printed output will look like. PySide6 is Python library for creating graphical user interfaces and it provides simple way to create print previews. in this article we want to learn how to create a print preview in PySide6.
How to Create Print Preview in Python PySide6
First of all you need to install Python PySide6 and you can install that using pip command like this
1 |
pip install PySide6 |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import sys from PySide6.QtCore import Qt, Slot, QMarginsF from PySide6.QtGui import QPageLayout, QPageSize, QAction from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit from PySide6.QtPrintSupport import QPrinter, QPrintPreviewDialog class MainWindow(QMainWindow): def __init__(self): super().__init__() self.MyUI() def MyUI(self): self.text_edit = QTextEdit(self) self.setCentralWidget(self.text_edit) # create print preview action print_preview_action = QAction("Print Preview", self) print_preview_action.triggered.connect(self.show_print_preview) self.toolbar = self.addToolBar("Print Preview") self.toolbar.addAction(print_preview_action) self.setWindowTitle("GeeksCoders - Preview Dialof") self.setGeometry(300, 300, 500, 400) @Slot() def show_print_preview(self): printer = QPrinter(QPrinter.HighResolution) printer.setPageSize(QPageSize(QPageSize.A4)) printer.setPageLayout(QPageLayout(QPageSize.A4, QPageLayout.Portrait, QMarginsF(10, 10, 10, 10))) preview_dialog = QPrintPreviewDialog(printer, self) preview_dialog.paintRequested.connect(self.print_preview) preview_dialog.exec() @Slot(QPrinter) def print_preview(self, printer): self.text_edit.print_(printer) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
In the above code first we have imported our required classes and modules, sys module is imported to handle command line arguments. QApplication is used to create new application instance, and QMainWindow is used to create the main window of the application.
QTextEdit is a widget for editing and displaying plain or rich text. setCentralWidget method is used to set the text editor as the central widget of the main window.
print preview action is created by defining a QAction object with label Print Preview. when the action is triggered, it calls the show_print_preview method of MainWindow class.
show_print_preview method creates new QPrinter object with high resolution setting and an A4 page size with 10mm margins. QPrintPreviewDialog is then created using QPrinter object and connected to print_preview slot.
print_preview slot is called when the QPrintPreviewDialog is shown and it prints the contents of the text editor onto the QPrinter object passed as a parameter.
And lastly QApplication is executed with sys.exit() to run the event loop and show the main window.
Run the code and this will be the result
Learn More on Python GUI
- How to Create Print Dialog in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6