Advertisement
Guest User

Untitled

a guest
Jul 8th, 2018
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.10 KB | None | 0 0
  1. import sys
  2. import os
  3. import platform
  4. import wx
  5.  
  6. # class My_Printout
  7. # class My_Frame
  8. # class My_App
  9.  
  10. #-------------------------------------------------------------------------------
  11.  
  12. if os.name == "posix":
  13. print("\nPlatform : UNIX - Linux")
  14. elif os.name in ['nt', 'dos', 'ce']:
  15. print("\nPlatform : Windows")
  16. else:
  17. print("\nPlatform : ", platform.system())
  18.  
  19. #-------------------------------------------------------------------------------
  20.  
  21. class My_Printout(wx.Printout):
  22. """
  23. Create a printout.
  24. """
  25. def __init__(self, text, title):
  26. wx.Printout.__init__(self, title)
  27.  
  28. #------------
  29.  
  30. self.lines = text
  31.  
  32. #---------------------------------------------------------------------------
  33.  
  34. def OnBeginDocument(self, start, end):
  35. """
  36. ...
  37. """
  38.  
  39. return super(My_Printout, self).OnBeginDocument(start, end)
  40.  
  41.  
  42. def OnEndDocument(self):
  43. """
  44. ...
  45. """
  46.  
  47. super(My_Printout, self).OnEndDocument()
  48.  
  49.  
  50. def OnBeginPrinting(self):
  51. """
  52. ...
  53. """
  54.  
  55. super(My_Printout, self).OnBeginPrinting()
  56.  
  57.  
  58. def OnEndPrinting(self):
  59. """
  60. ...
  61. """
  62.  
  63. super(My_Printout, self).OnEndPrinting()
  64.  
  65.  
  66. def OnPreparePrinting(self):
  67. """
  68. ...
  69. """
  70.  
  71. super(My_Printout, self).OnPreparePrinting()
  72.  
  73.  
  74. def HasPage(self, page):
  75. """
  76. ...
  77. """
  78.  
  79. if page <= 2:
  80. return True
  81. else:
  82. return False
  83.  
  84.  
  85. def GetPageInfo(self):
  86. """
  87. ...
  88. """
  89.  
  90. return (1, 2, 1, 2)
  91.  
  92.  
  93. def OnPrintPage(self, page):
  94. """
  95. ...
  96. """
  97.  
  98. dc = self.GetDC()
  99.  
  100. # (wx.MM_METRIC) ---> Each logical unit is 1 mm.
  101. # (wx.MM_POINTS) ---> Each logical unit is a "printer point" i.e.
  102. dc.SetMapMode(wx.MM_POINTS)
  103.  
  104. dc.SetTextForeground("red")
  105. dc.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD))
  106. dc.DrawText(self.lines, 50, 100)
  107.  
  108. # R, V, B.
  109. dc.SetPen(wx.Pen(wx.Colour(255, 20, 5)))
  110. dc.SetBrush(wx.Brush(wx.Colour(30, 255, 20)))
  111. # x, y, radius.
  112. dc.DrawCircle(100, 275, 25)
  113. # x, y, width, height.
  114. dc.DrawEllipse(100, 275, 75, 50)
  115.  
  116. return True
  117.  
  118. #-------------------------------------------------------------------------------
  119.  
  120. class My_Frame(wx.Frame):
  121. """
  122. Create a main frame for my application.
  123. """
  124. def __init__(self, parent, id, title=""):
  125. wx.Frame.__init__(self,
  126. parent,
  127. id,
  128. title,
  129. size=(600, 350),
  130. style=wx.DEFAULT_FRAME_STYLE)
  131.  
  132. #------------
  133.  
  134. # Simplified init method.
  135. self.SetProperties()
  136. self.CreateMenu()
  137. self.CreateCtrls()
  138. self.CreatePrintData()
  139. self.BindEvents()
  140. self.DoLayout()
  141.  
  142. #------------
  143.  
  144. self.CenterOnScreen()
  145.  
  146. #---------------------------------------------------------------------------
  147.  
  148. def SetProperties(self):
  149. """
  150. Set the main frame properties (title, icon...).
  151. """
  152.  
  153. self.SetTitle("Printing test...")
  154.  
  155.  
  156. def CreateMenu(self):
  157. """
  158. Make the frame menus.
  159. """
  160.  
  161. menub = wx.MenuBar()
  162.  
  163. fmenu = wx.Menu()
  164. fmenu.Append(wx.ID_PAGE_SETUP, "Page set&up\tCtrl+U")
  165. fmenu.Append(wx.ID_PREVIEW, "Print pre&view\tCtrl+V")
  166. fmenu.Append(wx.ID_PRINT, "&Print\tCtrl+P")
  167. fmenu.AppendSeparator()
  168. fmenu.Append(wx.ID_EXIT, "E&xit\tCtrl+X")
  169. menub.Append(fmenu, "&File")
  170.  
  171. self.SetMenuBar(menub)
  172.  
  173.  
  174. def CreateCtrls(self):
  175. """
  176. Make widgets for my app.
  177. """
  178.  
  179. font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  180. font.SetWeight(wx.BOLD)
  181. font.SetPointSize(10)
  182.  
  183. #------------
  184.  
  185. # First create the controls.
  186. self.panel = wx.Panel(self,
  187. id=-1,
  188. style=wx.BORDER_THEME|
  189. wx.TAB_TRAVERSAL)
  190.  
  191. self.text = wx.StaticText(self.panel,
  192. id=-1,
  193. label="Demonstrating :")
  194. self.text.SetFont(font)
  195.  
  196. self.info = wx.StaticText(self.panel,
  197. id=-1,
  198. label="1) Direct printing,\n"
  199. "2) Printout class,\n"
  200. "3) Preview,\n"
  201. "4) Menu,\n"
  202. "5) Page setup.")
  203. self.info.SetForegroundColour("red")
  204. font.SetWeight(wx.NORMAL)
  205. self.info.SetFont(font)
  206.  
  207. self.tc = wx.TextCtrl(self.panel,
  208. id=-1,
  209. size=(200, -1),
  210. value="Hello, World ! A sample text.")
  211.  
  212. self.btnSetup = wx.Button(self.panel,
  213. id=wx.ID_PAGE_SETUP,
  214. label="Page set&up")
  215.  
  216. self.btnPreview = wx.Button(self.panel,
  217. id=wx.ID_PREVIEW,
  218. label="Print pre&view")
  219. self.btnPreview.SetFocus()
  220.  
  221. self.btnPrint = wx.Button(self.panel,
  222. id=wx.ID_PRINT,
  223. label="&Print")
  224.  
  225. self.btnClose = wx.Button(self.panel,
  226. id=wx.ID_CLOSE,
  227. label="E&xit")
  228.  
  229.  
  230. def CreatePrintData(self):
  231. """
  232. Create printing data.
  233. """
  234.  
  235. self.printdata = wx.PrintData()
  236.  
  237. self.printdata.SetPrinterName('')
  238. self.printdata.SetOrientation(wx.PORTRAIT)
  239. self.printdata.SetPaperId(wx.PAPER_A4)
  240. self.printdata.SetQuality(wx.PRINT_QUALITY_DRAFT)
  241. # Black and white printing if False.
  242. self.printdata.SetColour(True)
  243. self.printdata.SetNoCopies(1)
  244. self.printdata.SetCollate(True)
  245. # self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
  246.  
  247.  
  248. def BindEvents(self):
  249. """
  250. Bind all the events related to my application.
  251. """
  252.  
  253. # Bind some menu events to an events handler.
  254. self.Bind(wx.EVT_MENU, self.OnBtnPageSetup, id=wx.ID_PAGE_SETUP)
  255. self.Bind(wx.EVT_MENU, self.OnBtnPreview, id=wx.ID_PREVIEW)
  256. self.Bind(wx.EVT_MENU, self.OnBtnPrint, id=wx.ID_PRINT)
  257. self.Bind(wx.EVT_MENU, self.OnBtnClose, id=wx.ID_EXIT)
  258.  
  259. # Bind the close event to an event handler.
  260. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  261.  
  262. # Bind some buttons events to an events handler.
  263. self.Bind(wx.EVT_BUTTON, self.OnBtnPageSetup, self.btnSetup)
  264. self.Bind(wx.EVT_BUTTON, self.OnBtnPreview, self.btnPreview)
  265. self.Bind(wx.EVT_BUTTON, self.OnBtnPrint, self.btnPrint)
  266. self.Bind(wx.EVT_BUTTON, self.OnBtnClose, self.btnClose)
  267.  
  268.  
  269. def DoLayout(self):
  270. """
  271. Manage widgets Layout.
  272. """
  273.  
  274. # MainSizer is the top-level one that manages everything.
  275. mainSizer = wx.BoxSizer(wx.VERTICAL)
  276.  
  277. #------------
  278.  
  279. hBox1 = wx.BoxSizer(wx.HORIZONTAL)
  280. hBox1.Add(self.info, 0, wx.ALL, 15)
  281.  
  282. #------------
  283.  
  284. hBox2 = wx.BoxSizer(wx.HORIZONTAL)
  285. hBox2.Add(self.btnSetup, 0, wx.ALL, 10)
  286. hBox2.Add(self.btnPreview, 0, wx.ALL, 10)
  287. hBox2.Add(self.btnPrint, 0, wx.ALL, 10)
  288. hBox2.Add(self.btnClose, 0, wx.ALL, 10)
  289.  
  290. #------------
  291.  
  292. mainSizer.Add(self.text, 0, wx.ALL, 10)
  293. mainSizer.Add(wx.StaticLine(self.panel),
  294. 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
  295. mainSizer.Add(self.tc, 0, wx.ALL, 15)
  296. mainSizer.Add(hBox1, 0, wx.ALL, 5)
  297. mainSizer.Add(hBox2, 0, wx.ALL, 5)
  298.  
  299. #------------
  300.  
  301. # Finally, tell the panel to use the mainSizer for layout.
  302. self.panel.SetSizer(mainSizer)
  303.  
  304.  
  305. def OnBtnPageSetup(self, event):
  306. """
  307. Show the PrinterSetup dialog.
  308. """
  309.  
  310. psdd = wx.PageSetupDialogData(self.printdata)
  311.  
  312. psdd.EnablePrinter(True)
  313. # psdd.CalculatePaperSizeFromId()
  314.  
  315. #------------
  316.  
  317. dlg = wx.PageSetupDialog(self, psdd)
  318. dlg.ShowModal()
  319.  
  320. #------------
  321.  
  322. # This makes a copy of the wx.PrintData instead of just saving
  323. # a reference to the one inside the PrintDialogData that will
  324. # be destroyed when the dialog is destroyed
  325. self.printdata = wx.PrintData(dlg.GetPageSetupData().GetPrintData())
  326.  
  327. dlg.Destroy()
  328.  
  329.  
  330. def OnBtnPreview(self, event):
  331. """
  332. Show the print preview.
  333. """
  334.  
  335. text = self.tc.GetValue()
  336.  
  337. #------------
  338.  
  339. data = wx.PrintDialogData(self.printdata)
  340.  
  341. printout1 = My_Printout(text, "- My printing object")
  342. printout2 = My_Printout(text, "- My printing object")
  343.  
  344. printPreview = wx.PrintPreview(printout1, printout2, data)
  345.  
  346. # Initial zoom value.
  347. if "__WXMAC__" in wx.PlatformInfo:
  348. printPreview.SetZoom(50)
  349. else:
  350. printPreview.SetZoom(35)
  351.  
  352. if not printPreview.IsOk():
  353. wx.MessageBox(("There was a problem printing.\nPerhaps "\
  354. "your current printer is \nnot "\
  355. "set correctly ?"),
  356. ("Printing"),
  357. wx.OK)
  358. return
  359.  
  360. else:
  361. previewFrame = wx.PreviewFrame(printPreview, None, "Print preview")
  362. previewFrame.Initialize()
  363. previewFrame.SetPosition(self.GetPosition())
  364. previewFrame.SetSize(self.GetSize())
  365. # Or full screen :
  366. # previewFrame.Maximize()
  367. previewFrame.Show(True)
  368. previewFrame.Layout()
  369.  
  370.  
  371. def OnBtnPrint(self, event):
  372. """
  373. Prints the document.
  374. """
  375.  
  376. text = self.tc.GetValue()
  377.  
  378. #------------
  379.  
  380. pdd = wx.PrintDialogData(self.printdata)
  381. pdd.SetPrintData(self.printdata)
  382. pdd.SetMinPage(1)
  383. pdd.SetMaxPage(1)
  384. pdd.SetFromPage(1)
  385. pdd.SetToPage(1)
  386. pdd.SetPrintToFile(False)
  387. # pdd.SetSetupDialog(False)
  388. # pdd.EnableSelection(True)
  389. # pdd.EnablePrintToFile(True)
  390. # pdd.EnablePageNumbers(True)
  391. # pdd.SetAllPages(True)
  392.  
  393. #------------
  394.  
  395. printer = wx.Printer(pdd)
  396.  
  397. myPrintout = My_Printout(text, "- My printing object")
  398.  
  399. if not printer.Print(self, myPrintout, True):
  400. wx.MessageBox(("There was a problem printing.\nPerhaps "\
  401. "your current printer is \nnot "\
  402. "set correctly ?"),
  403. ("Printing"),
  404. wx.OK)
  405. return
  406.  
  407. else:
  408. self.printData = wx.PrintData(printer.GetPrintDialogData().GetPrintData())
  409. myPrintout.Destroy()
  410.  
  411.  
  412. def OnBtnClose(self, event):
  413. """
  414. ...
  415. """
  416.  
  417. self.Close(True)
  418.  
  419.  
  420. def OnCloseWindow(self, event):
  421. """
  422. ...
  423. """
  424.  
  425. self.Destroy()
  426.  
  427. #-------------------------------------------------------------------------------
  428.  
  429. class My_App(wx.App):
  430. """
  431. ...
  432. """
  433. def OnInit(self):
  434.  
  435. #------------
  436.  
  437. self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
  438.  
  439. #------------
  440.  
  441. frame = My_Frame(None, id=-1)
  442. self.SetTopWindow(frame)
  443. frame.Show(True)
  444.  
  445. return True
  446.  
  447.  
  448. #-------------------------------------------------------------------------------
  449.  
  450. def main():
  451. app = My_App(False)
  452. app.MainLoop()
  453.  
  454. #-------------------------------------------------------------------------------
  455.  
  456. if __name__ == "__main__" :
  457. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement