Using wxPython

First things first...

I'm not going to try and teach the Python language here. You can do that at the Python Tutorial (#2). I'm also going to assume that you know a bit about wxWidgets already, enough to notice the similarities in the classes used.

Take a look at the following wxPython program. You can find a similar program in the wxPython/demo directory, named DialogUnits.py. If your Python and wxPython are properly installed, you should be able to run it by issuing this command:

python DialogUnits.py


001: ## import all of the wxPython GUI package
002: from wxPython.wx import *
003:
004: ## Create a new frame class, derived from the wxPython Frame.
005: class MyFrame(wxFrame):
006:
007:     def __init__(self, parent, id, title):
008:         # First, call the base class' __init__ method to create the frame
009:         wxFrame.__init__(self, parent, id, title,
010:                          wxPoint(100, 100), wxSize(160, 100))
011:
012:         # Associate some events with methods of this class
013:         EVT_SIZE(self, self.OnSize)
014:         EVT_MOVE(self, self.OnMove)
015:
016:         # Add a panel and some controls to display the size and position
017:         panel = wxPanel(self, -1)
018:         wxStaticText(panel, -1, "Size:",
019:                      wxDLG_PNT(panel, wxPoint(4, 4)),  wxDefaultSize)
020:         wxStaticText(panel, -1, "Pos:",
021:                      wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
022:         self.sizeCtrl = wxTextCtrl(panel, -1, "",
023:                                    wxDLG_PNT(panel, wxPoint(24, 4)),
024:                                    wxDLG_SZE(panel, wxSize(36, -1)),
025:                                    wxTE_READONLY)
026:         self.posCtrl = wxTextCtrl(panel, -1, "",
027:                                   wxDLG_PNT(panel, wxPoint(24, 14)),
028:                                   wxDLG_SZE(panel, wxSize(36, -1)),
029:                                   wxTE_READONLY)
030:
031:
032:     # This method is called automatically when the CLOSE event is
033:     # sent to this window
034:     def OnCloseWindow(self, event):
035:         # tell the window to kill itself
036:         self.Destroy()
037:
038:     # This method is called by the system when the window is resized,
039:     # because of the association above.
040:     def OnSize(self, event):
041:         size = event.GetSize()
042:         self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
043:
044:         # tell the event system to continue looking for an event handler,
045:         # so the default handler will get called.
046:         event.Skip()
047:
048:     # This method is called by the system when the window is moved,
049:     # because of the association above.
050:     def OnMove(self, event):
051:         pos = event.GetPosition()
052:         self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
053:
054:
055: # Every wxWidgets application must have a class derived from wxApp
056: class MyApp(wxApp):
057:
058:     # wxWidgets calls this method to initialize the application
059:     def OnInit(self):
060:
061:         # Create an instance of our customized Frame class
062:         frame = MyFrame(NULL, -1, "This is a test")
063:         frame.Show(true)
064:
065:         # Tell wxWidgets that this is our main window
066:         self.SetTopWindow(frame)
067:
068:         # Return a success flag
069:         return true
070:
071:
072: app = MyApp(0)     # Create an instance of the application class
073: app.MainLoop()     # Tell it to start processing events
074:

Things to notice

  1. At line 2 the wxPython classes, constants, and etc. are imported into the current module's namespace. If you prefer to reduce namespace pollution you can use "from wxPython import wx" and then access all the wxPython identifiers through the wx module, for example, "wx.wxFrame".
  2. At line 13 the frame's sizing and moving events are connected to methods of the class. These helper functions are intended to be like the event table macros that wxWidgets employs. But since static event tables are impossible with wxPython, we use helpers that are named the same to dynamically build the table. The only real difference is that the first argument to the event helpers is always the window that the event table entry should be added to.
  3. Notice the use of wxDLG_PNT and wxDLG_SZE in lines 19 - 29 to convert from dialog units to pixels. These helpers are unique to wxPython since Python can't do method overloading like C++.
  4. There is an OnCloseWindow method at line 34 but no call to EVT_CLOSE to attach the event to the method. Does it really get called? The answer is, yes it does. This is because many of the standard events are attached to windows that have the associated standard method names. I have tried to follow the lead of the C++ classes in this area to determine what is standard but since that changes from time to time I can make no guarantees, nor will it be fully documented. When in doubt, use an EVT_*** function.
  5. At lines 17 to 21 notice that there are no saved references to the panel or the static text items that are created. Those of you who know Python might be wondering what happens when Python deletes these objects when they go out of scope. Do they disappear from the GUI? They don't. Remember that in wxPython the Python objects are just shadows of the corresponding C++ objects. Once the C++ windows and controls are attached to their parents, the parents manage them and delete them when necessary. For this reason, most wxPython objects do not need to have a __del__ method that explicitly causes the C++ object to be deleted. If you ever have the need to forcibly delete a window, use the Destroy() method as shown on line 36.
  6. Just like wxWidgets in C++, wxPython apps need to create a class derived from wxApp (line 56) that implements a method named OnInit, (line 59.) This method should create the application's main window (line 62) and use wxApp.SetTopWindow() (line 66) to inform wxWidgets about it.
  7. And finally, at line 72 an instance of the application class is created. At this point wxPython finishes initializing itself, and calls the OnInit method to get things started. (The zero parameter here is a flag for functionality that isn't quite implemented yet. Just ignore it for now.) The call to MainLoop at line 73 starts the event loop which continues until the application terminates or all the top level windows are closed.

ymasuda 平成17年11月19日