How to Use wxPython Demo Code

This wiki entry will explain how to use some code from the wxPython Demo in your own project. I'm going to use the wx.AboutBox in this example.

Open up Python and create a file with the following contents:

   1 import wx
   2  
   3 class MyApp(wx.App):
   4     def __init__(self, redirect=False, filename=None):
   5         wx.App.__init__(self, redirect, filename)
   6         self.frame = wx.Frame(None, wx.ID_ANY, title='My Title')
   7  
   8         self.panel = wx.Panel(self.frame, wx.ID_ANY)
   9  
  10 if __name__ == '__main__':
  11     app = MyApp()
  12     app.MainLoop()

Now, open the wxPython Demo (if you don't have it, you can download it here: http://wxpython.org/download.php#demo) and do the following

Example

   1 import wx
   2 from wx.lib.wordwrap import wordwrap
   3 
   4 class MyApp(wx.App):
   5    def __init__(self, redirect=False, filename=None):
   6        wx.App.__init__(self, redirect, filename)
   7        self.frame = wx.Frame(None, wx.ID_ANY, title='My Title')
   8 
   9        self.panel = wx.Panel(self.frame, wx.ID_ANY)
  10 
  11        # copy the code for the AboutBox
  12 
  13        # change the button's parent to refer to my panel
  14        b = wx.Button(self.panel, -1, "Show a wx.AboutBox", (50,50))
  15        self.Bind(wx.EVT_BUTTON, self.OnButton, b)
  16 
  17        self.frame.Show()
  18 
  19    def OnButton(self, evt):
  20        # First we create and fill the info object
  21        info = wx.AboutDialogInfo()
  22        info.Name = "Hello World"
  23        info.Version = "1.2.3"
  24        info.Copyright = "(C) 2006 Programmers and Coders Everywhere"
  25        info.Description = wordwrap(
  26            "A \"hello world\" program is a software program that prints out "
  27            "\"Hello world!\" on a display device. It is used in many introductory "
  28            "tutorials for teaching a programming language."
  29                      "\n\nSuch a program is typically one of the simplest programs possible "
  30            "in a computer language. A \"hello world\" program can be a useful "
  31            "sanity test to make sure that a language's compiler, development "
  32            "environment, and run-time environment are correctly installed.",
  33            # change the wx.ClientDC to use self.panel instead of self
  34            350, wx.ClientDC(self.panel))
  35        info.WebSite = ("http://en.wikipedia.org/wiki/Hello_world", "Hello World home page")
  36        info.Developers = [ "Joe Programmer",
  37                            "Jane Coder",
  38                            "Vippy the Mascot" ]
  39 
  40        # change the wx.ClientDC to use self.panel instead of self
  41        info.License = wordwrap(licenseText, 500, wx.ClientDC(self.panel))
  42 
  43        # Then we call wx.AboutBox giving it that info object
  44        wx.AboutBox(info)
  45 
  46 overview = """<html><body>
  47 <h2><center>wx.AboutBox</center></h2>
  48 
  49 This function shows the native standard about dialog containing the
  50 information specified in info. If the current platform has a native
  51 about dialog which is capable of showing all the fields in info, the
  52 native dialog is used, otherwise the function falls back to the
  53 generic wxWidgets version of the dialog.
  54 
  55 </body></html>
  56 """
  57 
  58 
  59 licenseText = "blah " * 250 + "\n\n" +"yadda " * 100
  60 
  61 if __name__ == '__main__':
  62    app = MyApp()
  63    app.MainLoop()

Basically, I copied the "from x import" line and then everything from the instantiation of the button down to the end of the button event handler. I also copied the "overview" and "licenseText" variables. Then I changed every instance where "self" referred to the Panel object in the demo code to "self.panel" so it would match my panel object.

Hopefully all that is clear. If you have questions, post to the wxPython list: http://wxpython.org/maillist.php

Using wxPython Demo Code (last edited 2010-07-12 18:27:29 by c-98-246-90-205)

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