subclassing wxMenuBar...

Bjorn Pettersen BPettersen at NAREX.com
Fri Jun 1 19:46:17 EDT 2001


I was stepping through the wxPython tutorial, and came across the part
where they're creating the menus. It seemed like a good idea to put the
menu definition in a datafile instead of specifying it programatically,
so I though it would be a good chance to get some xml experience too...

I have the following xml:

  <menu name="_File">
    <menuitem id="101" name="_About" info="More information about this
program"/>
    <separator/>
    <menuitem id="102" name="E_xit" info="Terminate the program"/>
  </menu>

(using the '_' instead of '&' to indicate shortcuts) and the following
code:

  from xml.dom import minidom
  from wxPython.wx import *
  
  def filterText(nodelist):
     return [ x for x in nodelist if x.nodeType != x.TEXT_NODE ]
  
  class XMLMenuBar(wxMenuBar):
     def __init__(self, filename):
        menubar = minidom.parse(filename)
        for menu in menubar.childNodes:
           menuName = menu.attributes['name'].nodeValue.replace('_',
'&')
           wxmenu = wxMenu()
           
           for menuItem in filterText(menu.childNodes):
              if menuItem.tagName == 'menuitem':
                 attrs = menuItem.attributes
                 idTag = int( attrs['id'].nodeValue )
                 name = attrs['name'].nodeValue.replace('_', '&')
                 info = attrs['info'].nodeValue
                 wxmenu.Append(idTag, name, info)
              elif menuItem.tagName == 'separator':
                 wxmenu.AppendSeparator()
                 
           self.Append(wxmenu, menuName)

(I'm relatively new to dom programming, so any hints about that would be
well received...) however, when I instantiate this class from my wxFrame
subclass, I get the following traceback from the last line above:

Traceback (most recent call last):
  File "wXMLMenu.py", line 65, in ?
    app = MyApp(0)
  File "d:\python21\wxPython\wx.py", line 1613, in __init__
    _wxStart(self.OnInit)
  File "wXMLMenu.py", line 60, in OnInit
    frame = MyFrame(NULL, -1, "Hello from wxPython")
  File "wXMLMenu.py", line 38, in __init__
    menu = XMLMenuBar('menu.xml')
  File "wXMLMenu.py", line 25, in __init__
    self.Append(wxmenu, menuName)
  File "d:\python21\wxPython\windows.py", line 807, in Append
    val = apply(windowsc.wxMenuBar_Append,(self,) + _args, _kwargs)
TypeError: Type error in argument 1 of wxMenuBar_Append. Expected
_wxMenuBar_p.

which I'm assuming means that the SWIGd code is expecting a wxMenuBar
pointer to be the first argument to Append and can't deal with a
subclass...

Does anyone know a way around this?

-- bjorn




More information about the Python-list mailing list