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
pip install PySide6
This is the complete code
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
self.text_edit = QTextEdit(self)
# create print preview action
print_preview_action = QAction("Print Preview", self)
self.toolbar = self.addToolBar("Print Preview")
self.setWindowTitle("GeeksCoders - Preview Dialof")
self.setGeometry(300, 300, 500, 400)
printer = QPrinter(QPrinter.HighResolution)
printer.setPageLayout(QPageLayout(QPageSize.A4, QPageLayout.Portrait, QMarginsF(10, 10, 10, 10)))
preview_dialog = QPrintPreviewDialog(printer, self)
def print_preview(self, printer):
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
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