In this Python GUI article i want to show you creating PrintDialog in wxPython. so using a PrintDialog, you will have a nice dialog for printing documents.
What is PrintDialog in wxPython?
PrintDialog in wxPython is a class, and that is used for creating standard print dialog box. This dialog box allows the user to select printer settings, such as which printer to use, the range of pages to print and the number of copies. It is part of the printing framework in wxPython, and it provides a way to manage and control the printing process.
Python GUI PrintDialog in wxPython
Let’s create our example, so now this is the complete code for Python GUI PrintDialog in wxPython.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(800, 600)) # Set icon for the frame self.SetIcon(wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG)) # Initialize panel self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) # Create button self.button = wx.Button(self, label="Print Dialog", pos=(100, 100)) # Bind button click event to the openDialog method self.Bind(wx.EVT_BUTTON, self.openDialog) def openDialog(self, event): # Create PrintDialogData object to configure the print dialog data = wx.PrintDialogData() # Enable different options in the print dialog data.EnableSelection(True) data.EnablePrintToFile(True) data.EnablePageNumbers(True) data.SetMinPage(1) data.SetMaxPage(10) # Create and show the print dialog dialog = wx.PrintDialog(self, data) if dialog.ShowModal() == wx.ID_OK: # If the user clicked OK, retrieve and print the print dialog data data = dialog.GetPrintDialogData() print('GetAllPages: %d\n' % data.GetAllPages()) # Destroy the dialog to free resources dialog.Destroy() class MyApp(wx.App): def OnInit(self): # Initialize main frame self.frame = MyFrame(parent=None, title="Codeloop.org - Print Dialog") self.frame.Show() return True # Entry point of the application app = MyApp() app.MainLoop() |
At the top we have created our MyFrame. this class is a top level window that inherits from wx.Frame and we create the object of our MyPanel class in here.
1 2 3 4 5 6 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (800,600)) self.panel = MyPanel(self) |
This is our MyPanel class that inherits from wx.Panel and we create a button in this class also we create the event binding process in this class.
1 2 3 4 5 6 7 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) self.button = wx.Button(self, label = "Print Dialog", pos = (100,100)) self.Bind(wx.EVT_BUTTON, self.openDialog) |
This is method is for creating of our PrintDialog, and at the top you can see that we have bonded this method with our button. so when a person click on the button a PrintDialog will be opened.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def openDialog(self, event): data = wx.PrintDialogData() data.EnableSelection(True) data.EnablePrintToFile(True) data.EnablePageNumbers(True) data.SetMinPage(1) data.SetMaxPage(10) dialog = wx.PrintDialog(self, data) #dialog.ShowModal() if dialog.ShowModal() == wx.ID_OK: data = dialog.GetPrintDialogData() print('GetAllPages: %d\n' % data.GetAllPages()) dialog.Destroy() |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop - wxPython Print Dialog") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email
How can I pass this code to Tkinter?
you can see my tkinter tutorials