Printing framework - Part 5 (Phoenix)

Keywords : Webbrowser, Os.startfile, Printing, Reading, PDF.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Printing is an essential element for your programs, here we show you how to print.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


PDF file reading and printing :

First example

img_sample_one_a.png

PDF file : wxpython.pdf

   1 # sample_one_a.py
   2 
   3 import sys
   4 import os
   5 import platform
   6 import wx
   7 
   8 # class My_Frame
   9 # class My_App
  10 
  11 #-------------------------------------------------------------------------------
  12 
  13 if os.name == "posix":
  14     print("\nPlatform : UNIX - Linux")
  15 elif os.name in ['nt', 'dos', 'ce']:
  16     print("\nPlatform : Windows")
  17 else:
  18     print("\nPlatform : ", platform.system())
  19 
  20 #-------------------------------------------------------------------------------
  21 
  22 class My_Frame(wx.Frame):
  23     """
  24     Create a main frame for my application.
  25     """
  26     def __init__(self, parent, id, title=""):
  27         wx.Frame.__init__(self,
  28                           parent,
  29                           id,
  30                           title,
  31                           size=(350, 300),
  32                           style=wx.DEFAULT_FRAME_STYLE)
  33 
  34         #------------
  35 
  36         # Simplified init method.
  37         self.SetProperties()
  38         self.CreateMenu()
  39         self.CreateCtrls()
  40         self.BindEvents()
  41         self.DoLayout()
  42 
  43         #------------
  44 
  45         self.CenterOnScreen()
  46 
  47     #---------------------------------------------------------------------------
  48 
  49     def SetProperties(self):
  50         """
  51         Set the main frame properties (title, icon...).
  52         """
  53 
  54         frameicon = wx.Icon("Icons/wxWidgets.ico")
  55         self.SetIcon(frameicon)
  56 
  57         #------------
  58 
  59         self.SetTitle("Reading test...")
  60 
  61 
  62     def CreateMenu(self):
  63         """
  64         Make the frame menus.
  65         """
  66 
  67         menub = wx.MenuBar()
  68 
  69         fmenu = wx.Menu()
  70         fmenu.Append(-1, "Sho&w PDF\tCtrl+W")
  71         fmenu.AppendSeparator()
  72         fmenu.Append(wx.ID_EXIT, "E&xit\tCtrl+X")
  73         menub.Append(fmenu, "&File")
  74 
  75         self.SetMenuBar(menub)
  76 
  77 
  78     def CreateCtrls(self):
  79         """
  80         Make widgets for my application.
  81         """
  82 
  83         boldFont = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  84         boldFont.SetWeight(wx.BOLD)
  85         boldFont.SetPointSize(10)
  86 
  87         #------------
  88 
  89         # First create the controls.
  90         self.panel = wx.Panel(self,
  91                               id=-1,
  92                               style=wx.BORDER_THEME|
  93                               wx.TAB_TRAVERSAL)
  94 
  95         self.text = wx.StaticText(self.panel,
  96                                   id=-1,
  97                                   label="Demonstrating :")
  98         self.text.SetFont(boldFont)
  99 
 100         self.info = wx.StaticText(self.panel,
 101                                   id=-1,
 102                                   label="> Read PDF file with installed default program.")
 103         self.info.SetForegroundColour("red")
 104 
 105         self.btnView = wx.Button(self.panel,
 106                                  id=-1,
 107                                  label="Sho&w PDF")
 108         self.btnView.SetFocus()
 109 
 110         self.btnClose = wx.Button(self.panel,
 111                                   id=wx.ID_CLOSE,
 112                                   label="E&xit")
 113 
 114 
 115     def BindEvents(self):
 116         """
 117         Bind all the events related to my application.
 118         """
 119 
 120         # Bind some menu events to an events handler.
 121         self.Bind(wx.EVT_MENU, self.OnBtnViewPdf, id=-1)
 122         self.Bind(wx.EVT_MENU, self.OnBtnClose, id=wx.ID_EXIT)
 123 
 124         # Bind the close event to an event handler.
 125         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 126 
 127         # Bind some buttons events to an events handler.
 128         self.Bind(wx.EVT_BUTTON, self.OnBtnViewPdf, self.btnView)
 129         self.Bind(wx.EVT_BUTTON, self.OnBtnClose, self.btnClose)
 130 
 131 
 132     def DoLayout(self):
 133         """
 134         Manage widgets Layout.
 135         """
 136 
 137         # MainSizer is the top-level one that manages everything.
 138         mainSizer = wx.BoxSizer(wx.VERTICAL)
 139 
 140         #------------
 141 
 142         hBox1 = wx.BoxSizer(wx.HORIZONTAL)
 143         hBox1.Add(self.info, 0, wx.ALL, 15)
 144 
 145         #------------
 146 
 147         hBox2 = wx.BoxSizer(wx.HORIZONTAL)
 148         hBox2.Add(self.btnView, 0, wx.ALL, 10)
 149         hBox2.Add(self.btnClose, 0, wx.ALL, 10)
 150 
 151         #------------
 152 
 153         mainSizer.Add(self.text, 0, wx.ALL, 10)
 154         mainSizer.Add(wx.StaticLine(self.panel),
 155                       0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
 156         mainSizer.Add(hBox1, 0, wx.ALL, 5)
 157         mainSizer.Add(hBox2, 0, wx.ALL, 5)
 158 
 159         #------------
 160 
 161         # Finally, tell the panel to use the mainSizer for layout.
 162         self.panel.SetSizer(mainSizer)
 163 
 164 
 165     def OnBtnViewPdf(self, event):
 166         """
 167         Launch a PDF program to read the file.
 168         """
 169 
 170         wx.BeginBusyCursor()
 171         os.startfile("wxpython.pdf")
 172         wx.CallAfter(wx.EndBusyCursor)
 173 
 174 
 175     def OnBtnClose(self, event):
 176         """
 177         ...
 178         """
 179 
 180         self.Close(True)
 181 
 182 
 183     def OnCloseWindow(self, event):
 184         """
 185         ...
 186         """
 187 
 188         self.Destroy()
 189 
 190 #-------------------------------------------------------------------------------
 191 
 192 class My_App(wx.App):
 193     """
 194     ...
 195     """
 196     def OnInit(self):
 197 
 198         #------------
 199 
 200         frame = My_Frame(None, id=-1)
 201         self.SetTopWindow(frame)
 202         frame.Show(True)
 203 
 204         return True
 205 
 206 #-------------------------------------------------------------------------------
 207 
 208 def main():
 209     app = My_App(False)
 210     app.MainLoop()
 211 
 212 #-------------------------------------------------------------------------------
 213 
 214 if __name__ == "__main__" :
 215     main()


Second example

img_sample_one_b.png

PDF file : wxpython.pdf

   1 # sample_one_b.py
   2 
   3 import sys
   4 import os
   5 import platform
   6 import wx
   7 import webbrowser
   8 
   9 # class My_Frame
  10 # class My_App
  11 
  12 #-------------------------------------------------------------------------------
  13 
  14 if os.name == "posix":
  15     print("\nPlatform : UNIX - Linux")
  16 elif os.name in ['nt', 'dos', 'ce']:
  17     print("\nPlatform : Windows")
  18 else:
  19     print("\nPlatform : ", platform.system())
  20 
  21 #-------------------------------------------------------------------------------
  22 
  23 class My_Frame(wx.Frame):
  24     """
  25     Create a main frame for my application.
  26     """
  27     def __init__(self, parent, id, title=""):
  28         wx.Frame.__init__(self,
  29                           parent,
  30                           id,
  31                           title,
  32                           size=(350, 300),
  33                           style=wx.DEFAULT_FRAME_STYLE)
  34 
  35         #------------
  36 
  37         # Simplified init method.
  38         self.SetProperties()
  39         self.CreateMenu()
  40         self.CreateCtrls()
  41         self.BindEvents()
  42         self.DoLayout()
  43 
  44         #------------
  45 
  46         self.CenterOnScreen()
  47 
  48     #---------------------------------------------------------------------------
  49 
  50     def SetProperties(self):
  51         """
  52         Set the main frame properties (title, icon...).
  53         """
  54 
  55         frameicon = wx.Icon("Icons/wxWidgets.ico")
  56         self.SetIcon(frameicon)
  57 
  58         #------------
  59 
  60         self.SetTitle("Reading test...")
  61 
  62 
  63     def CreateMenu(self):
  64         """
  65         Make the frame menus.
  66         """
  67 
  68         menub = wx.MenuBar()
  69 
  70         fmenu = wx.Menu()
  71         fmenu.Append(-1, "Sho&w PDF\tCtrl+W")
  72         fmenu.AppendSeparator()
  73         fmenu.Append(wx.ID_EXIT, "E&xit\tCtrl+X")
  74         menub.Append(fmenu, "&File")
  75 
  76         self.SetMenuBar(menub)
  77 
  78 
  79     def CreateCtrls(self):
  80         """
  81         Make widgets for my application.
  82         """
  83 
  84         boldFont = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  85         boldFont.SetWeight(wx.BOLD)
  86         boldFont.SetPointSize(10)
  87 
  88         #------------
  89 
  90         # First create the controls.
  91         self.panel = wx.Panel(self,
  92                               id=-1,
  93                               style=wx.BORDER_THEME|
  94                               wx.TAB_TRAVERSAL)
  95 
  96         self.text = wx.StaticText(self.panel,
  97                                   id=-1,
  98                                   label="Demonstrating :")
  99         self.text.SetFont(boldFont)
 100 
 101         self.info = wx.StaticText(self.panel,
 102                                   id=-1,
 103                                   label="> Read PDF file with default web browser.")
 104         self.info.SetForegroundColour("red")
 105 
 106         self.btnView = wx.Button(self.panel,
 107                                  id=-1,
 108                                  label="Sho&w PDF")
 109         self.btnView.SetFocus()
 110 
 111         self.btnClose = wx.Button(self.panel,
 112                                   id=wx.ID_CLOSE,
 113                                   label="E&xit")
 114 
 115 
 116     def BindEvents(self):
 117         """
 118         Bind all the events related to my application.
 119         """
 120 
 121         # Bind some menu events to an events handler.
 122         self.Bind(wx.EVT_MENU, self.OnBtnViewPdf, id=-1)
 123         self.Bind(wx.EVT_MENU, self.OnBtnClose, id=wx.ID_EXIT)
 124 
 125         # Bind the close event to an event handler.
 126         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 127 
 128         # Bind some buttons events to an events handler.
 129         self.Bind(wx.EVT_BUTTON, self.OnBtnViewPdf, self.btnView)
 130         self.Bind(wx.EVT_BUTTON, self.OnBtnClose, self.btnClose)
 131 
 132 
 133     def DoLayout(self):
 134         """
 135         Manage widgets Layout.
 136         """
 137 
 138         # MainSizer is the top-level one that manages everything.
 139         mainSizer = wx.BoxSizer(wx.VERTICAL)
 140 
 141         #------------
 142 
 143         hBox1 = wx.BoxSizer(wx.HORIZONTAL)
 144         hBox1.Add(self.info, 0, wx.ALL, 15)
 145 
 146         #------------
 147 
 148         hBox2 = wx.BoxSizer(wx.HORIZONTAL)
 149         hBox2.Add(self.btnView, 0, wx.ALL, 10)
 150         hBox2.Add(self.btnClose, 0, wx.ALL, 10)
 151 
 152         #------------
 153 
 154         mainSizer.Add(self.text, 0, wx.ALL, 10)
 155         mainSizer.Add(wx.StaticLine(self.panel),
 156                       0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
 157         mainSizer.Add(hBox1, 0, wx.ALL, 5)
 158         mainSizer.Add(hBox2, 0, wx.ALL, 5)
 159 
 160         #------------
 161 
 162         # Finally, tell the panel to use the mainSizer for layout.
 163         self.panel.SetSizer(mainSizer)
 164 
 165 
 166     def OnBtnViewPdf(self, event):
 167         """
 168         Launch a PDF program to read the file.
 169         """
 170 
 171         wx.BeginBusyCursor()
 172         webbrowser.open_new("wxpython.pdf")
 173         wx.CallAfter(wx.EndBusyCursor)
 174 
 175 
 176     def OnBtnClose(self, event):
 177         """
 178         ...
 179         """
 180 
 181         self.Close(True)
 182 
 183 
 184     def OnCloseWindow(self, event):
 185         """
 186         ...
 187         """
 188 
 189         self.Destroy()
 190 
 191 #-------------------------------------------------------------------------------
 192 
 193 class My_App(wx.App):
 194     """
 195     ...
 196     """
 197     def OnInit(self):
 198 
 199         #------------
 200 
 201         frame = My_Frame(None, id=-1)
 202         self.SetTopWindow(frame)
 203         frame.Show(True)
 204 
 205         return True
 206 
 207 #-------------------------------------------------------------------------------
 208 
 209 def main():
 210     app = My_App(False)
 211     app.MainLoop()
 212 
 213 #-------------------------------------------------------------------------------
 214 
 215 if __name__ == "__main__" :
 216     main()


Download source

source.zip


Additional Information

Link :

https://wiki.wxpython.org/MoreCommentsOnPrinting

https://wiki.wxpython.org/PrintingWithReportGenerators

http://www.blog.pythonlibrary.org/2012/06/17/an-intro-to-rst2pdf-changing-restructured-text-into-pdfs-with-python/

http://www.blog.pythonlibrary.org/2010/05/15/manipulating-pdfs-with-python-and-pypdf/

https://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Robin Dunn.


About this page

Date (d/m/y) Person (bot) Comments :

10/10/18 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah...

Printing framework - Part 5 (Phoenix) (last edited 2020-10-07 10:38:54 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.