Python Study Notes-wxpython print preview

Source: Internet
Author: User

Learn wxPython in action and Demo programs and take some study notes.

WxPython prints by using device context and drawing operations. An important class for printing in wxPython: wx. PrintOut, which manages the actual image part. The Print Output instance can be managed by a wx. Printer object that represents the Printer or the wx. PrintPreview object that is used to print the preview. Next I will introduce the print preview.

1. Create a subclass of wx. PrintOut and overwrite the wx. PrintOut method to define your printing behavior. Wx. PrintOut has multiple methods that can overwrite to customize the printing behavior. These methods are automatically triggered and generally do not need to overwrite all methods. The following describes the methods by analyzing the classes in wxPython Demo.


[Python]
Class MyPrintout (wx. Printout ):
Def _ init _ (self, canvas, log ):
Wx. Printout. _ init _ (self)
Self. canvas = canvas
Self. log = log
Def OnBeginDocument (self, start, end): # the start and end parameters tell wxPython to print the start page and the last page.
Self. log. WriteText ("MyPrintout. OnBeginDocument \ n") # perform the operations you want
Return super (MyPrintout, self). OnBeginDocument (start, end) # The method of the parent class must be called, because the method of the parent class has to do some important work.
 
Def OnEndDocument (self): # triggered after the document copy is printed.
Self. log. WriteText ("MyPrintout. OnEndDocument \ n ")
Super (MyPrintout, self). OnEndDocument () # The method of the parent class must also be called.
 
Def OnBeginPrinting (self): # This method does not need to be reloaded in general.
Self. log. WriteText ("MyPrintout. OnBeginPrinting \ n ")
Super (MyPrintout, self). OnBeginPrinting ()
 
Def OnEndPrinting (self): # This method is called to end printing after the copy is printed.
Self. log. WriteText ("MyPrintout. OnEndPrinting \ n ")
Super (MyPrintout, self). OnEndPrinting ()
 
Def OnPreparePrinting (self): # It is usually used to print some previous calculations, such as how many pages are computed
Self. log. WriteText ("MyPrintout. OnPreparePrinting \ n ")
Super (MyPrintout, self). OnPreparePrinting ()
 
Def HasPage (self, page): # This method usually needs to be overwritten. It is used by the print framework for loop control. If the parameter page is within the scope of the document, True is returned; otherwise, False is returned.
Self. log. WriteText ("MyPrintout. HasPage: % d \ n" % page)
If page <= 2:
Return True
Else:
Return False
 
Def GetPageInfo (self): # Get the page number of the document
Self. log. WriteText ("MyPrintout. GetPageInfo \ n ")
Return (1, 2, 1, 2)
 
Def OnPrintPage (self, page): # actual printing operation
Self. log. WriteText ("MyPrintout. OnPrintPage: % d \ n" % page)
Dc = self. GetDC ()
...
Return True
Ii. Print and preview
Step 1: Create a preview instance
Wx. PrintPreview (printout, printoutForPrinting, data = None)

Parameter: printout is the wx. PrintOut object, used to manage preview;

PintoutForPrinting is also a wx. PrintOut object. If it is not set to None, a Print button is included in the window during actual Print preview. This button is used to start printing.

Data is a wx. PrintData object or wx. PrintDialogData object. If the parameter is specified, this parameter is used to control the print preview.

Step 2: Create a preview framework

With wx. PrintPreview, a framework is required to display wx. PrintPreview.

Wx. PreviewFrame (preView, parent, title, pos = wx. DefaultPosition, size = wx. DefaultSize, style = wx. DEFAULT_FRAME_STYLE, name = 'framework ')

PreView is the instance to be previewed.

Step 3: Initialize the framework

Before you display your wx. PreviewFrame, you need to call the Initialize () method to create the internal parts of the window and perform other internal calculations.

The following provides a simple bitmap printing function based on the interception of the previous video:


[Python]
#-*-Coding: UTF-8 -*-
Import wx
Import wx. xrc
Import VideoCapture
######################################## ###################################
# Class MyFrame1
######################################## ###################################
Class MyFrame1 (wx. Frame ):

Def _ init _ (self, parent ):
Wx. frame. _ init _ (self, parent, id = wx. ID_ANY, title = wx. emptyString, pos = wx. defaultPosition, size = wx. size (566,535), style = wx. DEFAULT_FRAME_STYLE | wx. TAB_TRAVERSAL)

Self. SetSizeHintsSz (wx. DefaultSize, wx. DefaultSize)

BSizer1 = wx. BoxSizer (wx. VERTICAL)

Self. m_panel1 = wx. Panel (self, wx. ID_ANY, wx. DefaultPosition, wx. DefaultSize, wx. TAB_TRAVERSAL)
BSizer1.Add (self. m_panel1, 1, wx. EXPAND | wx. ALL, 5)

Self. m_button3 = wx. Button (self, wx. ID_ANY, u "video capture", wx. DefaultPosition, wx. DefaultSize, 0)
Self. m_button4 = wx. Button (self, wx. ID_ANY, u "print preview", wx. DefaultPosition, wx. DefaultSize, 0)
BSizer1.Add (self. m_button3, 0, wx. ALL, 5)
BSizer1.Add (self. m_button4, 0, wx. ALL, 5)

Self. SetSizer (bSizer1)
Self. Layout ()

Self. Centre (wx. BOTH)
# Connect Events
Self. m_button3.Bind (wx. EVT_BUTTON, self. OnButton)
Self. m_button4.Bind (wx. EVT_BUTTON, self. OnButton2)
Self. timer = wx. Timer (self)
Self. Bind (wx. EVT_TIMER, self. OnIdel, self. timer)
Self. Bind (wx. EVT_CLOSE, self. OnExit)
Def OnExit (self, evnet ):
Self. timer. Stop ()
Wx. Exit ()
Pass
Def OnIdel (self, evnet ):
Cam = VideoCapture. Device ()
Self.cam.saveSnapshot('test.jpg ')
Img = wx. Image ("test.jpg", wx. BITMAP_TYPE_ANY). ConvertToBitmap ()
Dc = wx. ClientDC (self. m_panel1)
Dc. DrawBitmap (img, 0, 0, False)
Def OnButton (self, event ):
Self. cam = VideoCapture. Device ()
Export cam.savesnapshot('test.jpg ')
Self. timer. Start (100)
Def OnButton2 (self, event ):
Self. timer. Stop ()
Self. printData = wx. PrintData ()
Self. printData. SetPaperId (wx. PAPER_LETTER)
Self. printData. SetPrintMode (wx. PRINT_MODE_PRINTER)
Data = wx. PrintDialogData (self. printData)
Printout = MyPrintout (self)
Printout2 = MyPrintout (self)
Self. preview = wx. PrintPreview (printout, printout2, data) # Step 1 create a preview Device
If not self. preview. OK ():
Wx. MessageBox ("error ")
Return
Pfrm = wx. PreviewFrame (self. preview, self, "This is a print preview") # create a framework in step 2
Pfrm. Initialize () # Step 3 Initialize the framework www.2cto.com
Pfrm. SetPosition (self. GetPosition () # Set the default position
Pfrm. SetSize (self. GetSize () # Set the size of the print preview box.
Pfrm. Show (True)

Class MyPrintout (wx. Printout ):
Def _ init _ (self, canvas ):
Wx. Printout. _ init _ (self)
Self. canvas = canvas
 
Def OnBeginDocument (self, start, end ):
Return super (MyPrintout, self). OnBeginDocument (start, end)
 
Def OnEndDocument (self ):
Super (MyPrintout, self). OnEndDocument ()
 
Def OnBeginPrinting (self ):
Super (MyPrintout, self). OnBeginPrinting ()
 
Def OnEndPrinting (self ):
Super (MyPrintout, self). OnEndPrinting ()
 
Def OnPreparePrinting (self ):
Super (MyPrintout, self). OnPreparePrinting ()
 
Def OnPrintPage (self, page ):
Dc = self. GetDC ()
#-------------------------------------------
# One possible method of setting scaling factors...
Img = wx. Image ("test.jpg", wx. BITMAP_TYPE_ANY). ConvertToBitmap ()
Dc. DrawBitmap (img, 0, 0, False)
Return True
If _ name __= = '_ main __':
App = wx. App ()
Frame = MyFrame1 (None)
Frame. Show (True)
App. MainLoop ()
Effect

Author: kkxgx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.