Always try to boil your problem down to a small example. Chances are you'll
find your error in the process, and if not, you'll have something complete
for us to look at.
Chris,
Perhaps this will help (all within class mainFrame, which is a wx.Frame):
menuFile = wx.Menu(self)
menuFile.Append(self, 101, '&Open \tCtrl-O', 'Open a knowledge base', wx.ITEM_NORMAL)
menuFile.Bind(wx.EVT_MENU, self.OnMenuFileOpen, 101)
What kind of object is menuFile? What is self?
I believe that menuFile is an instance of wx.Menu(); I thought that 'self'
refered to the menu item, in this case with ID == 101.
Now the error I'm getting is:
File "/mnt/usr2/fuzzy/eikos/ui/eikosFrame1.py", line 16, in __init__
menuFile = wx.Menu(self, "")
File "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-ansi/wx/_core.py", line 10194, in __init__
newobj = _core_.new_Menu(*args, **kwargs)
TypeError: String or Unicode type required
When I look at the API docs, in wx.Menu-class.html I see nothing about
binding the event handler to the item (ID specified or not). In
wx.MenuEvent-class.html I read about menu items being opened, closed, or
highlighted. Still not what I need. In wx.MenuItem-class.html I cannot find a
reference to binding wx.EVT_MENU to a selected item.
Greping wx.EVT_MENU in that directory finds only the open, close, and
highlight references in wx.MenuEvent-class.html.
So, I've tried to find the documentation for this and so far have failed at
the attempt. The Wiki page has a single event binding and that didn't work
for me, either.
It's a bit uglier than it should be, because a menu item is not a true
wx.Window, and thus does not catch events itself, and does not have a
Bind() method (I still don't see why it couldn't, but there you go). You
need to catch the event in the parent frame. This is the cleanest way I
think there is to do it, as I don't like to use explicit IDs (untested):
FileMenu = wx.Menu(self, ....)
....
Item = Menu.Append(wx.ID_ANY, "Open" )
self.Bind(wx.EVT_MENU, self.OnFileOpen, Item)
So, what do I add to the instance FileMenu after 'self'? And, do I need to
make a separate assignment of menu.Append for each item in my menu?
.....
def OnFileOpen(self, event):
DoWhatEver()
What I have is:
def OnMenuFileOpen(self, event):
dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", wx.OPEN)
try:
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
self.model.LoadFile(filename)
self.FileName = filename
self.SetTitle(('Eikos - %s') % filename)
finally:
dlg.Destroy()
Am I getting closer?
Thanks,
Rich
···
On Mon, 21 Nov 2005, Chris Barker wrote:
--
Richard B. Shepard, Ph.D. | Author of "Quantifying Environmental
Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic"
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863