[wxPython] popup menu in wxOGL example question

Using the wxOGL example in the wxPython demo as a starting point, how would
I create a popup menu that would respond to right-clicking on a shape. The
menu items on the popup menu should do something to the shape, for instance
adding/removing a shadow.

I have code that does everything EXCEPT respond properly to clicking on the
menu item. The menu pops up when I right click on the shape, but the event
handlers are not called when I click on a menu item in the popup menu. (In
fact, I am getting a cryptic TypeError message sent to stdout - "TypeError:
too many arguments; expected 1, got 2" - no actual exception and no
indication where it occurs.)

Thanks for any help!
Nick

# example code follows

# constants declared at the top of the module
ID_SHAPE_SHADOW_ON = 210
ID_SHAPE_SHADOW_OFF = 211

#...snip...

    # the pop up menu is initialized in the __init__ method of
MyFrame(wxFrame)
    self.shape_menu = wxMenu()
    self.shape_menu.Append(ID_SHAPE_SHADOW_ON, "Shadow ON")
    self.shape_menu.Append(ID_SHAPE_SHADOW_OFF, "Shadow OFF")

    # handler declarations are shortly thereafter
    EVT_MENU(self, ID_SHAPE_SHADOW_ON, self.OnShadowOn)
    EVT_MENU(self, ID_SHAPE_SHADOW_OFF, self.OnShadowOff)

#...snip...

  # event handlers declared in body of MyFrame class
  def OnShadowOn(self):
    print "OnShadowOn called\n"
    # actual code removed for clarity

  def OnShadowOff(self):
    print "OnShadowOff called\n"
    # actual code removed for clarity

#...snip...

# the right-click event handler in MyShapeEvtHandler(wxShapeEvtHandler)

  def OnRightClick(self, x, y, *dontcare):
    self.OnLeftClick(x, y, 0, 0) # selects the shape under
the mouse
    self.UpdateStatusBar(shape)

    shape = self.GetShape()
    canvas = shape.GetCanvas()
    frame = self.statbarFrame

    # calculate where the popup menu should be drawn (it works,
but awaiting better response from mailing list)
    (scrollX, scrollY) = canvas.CalcScrolledPosition(x, y)
    (None, canvasY) = canvas.GetPositionTuple()
    (None, frameY) = frame.GetPositionTuple()

    # bring up the popup menu
    frame.PopupMenu(frame.shape_menu, wxPoint(scrollX, scrollY +
canvasY + frameY))

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

[SNIP]

I have code that does everything EXCEPT respond properly to clicking on

the

menu item. The menu pops up when I right click on the shape, but the

event

handlers are not called when I click on a menu item in the popup menu.

(In

fact, I am getting a cryptic TypeError message sent to stdout -

"TypeError:

too many arguments; expected 1, got 2" - no actual exception and no
indication where it occurs.)

[SNIP]

def OnShadowOn(self):
print "OnShadowOn called\n"
# actual code removed for clarity

def OnShadowOff(self):
print "OnShadowOff called\n"
# actual code removed for clarity

[SNIP]

I suspect that your problem is here. OnShadowOn and OnShadowOff will get
called with the menuEvent as an argument. Try:

def OnShadowOn(self, event):
   #...

I think that should fix it.

-tim

···

----- Original Message -----
From: "Newhard, Nick" <nnewhard@verant.com>

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

handlers are not called when I click on a menu item in the popup menu.

(In

fact, I am getting a cryptic TypeError message sent to stdout -

"TypeError:

too many arguments; expected 1, got 2" - no actual exception and no
indication where it occurs.)

Look for something that has only one parameter and should have two.

# event handlers declared in body of MyFrame class
def OnShadowOn(self):
print "OnShadowOn called\n"
# actual code removed for clarity

def OnShadowOff(self):
print "OnShadowOff called\n"
# actual code removed for clarity

Voila! All event handlers are given an event object argument, even if they
don't need to use it. Try this:

def OnShadowOn(self, evt):
     print "OnShadowOn called\n"
     # actual code removed for clarity

def OnShadowOff(self, evt):
     print "OnShadowOff called\n"
     # actual code removed for clarity

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Using the wxOGL example in the wxPython demo as a starting point, how would
I create a popup menu that would respond to right-clicking on a shape. The
menu items on the popup menu should do something to the shape, for instance
adding/removing a shadow.

Have fun... I'm doing something similar, and my popup menus don't seem to
be working properly - whatever shape I click on first, I get its popup
menu when I subsequently click on other shapes...

I have code that does everything EXCEPT respond properly to clicking on the
menu item. The menu pops up when I right click on the shape, but the event
handlers are not called when I click on a menu item in the popup menu. (In
fact, I am getting a cryptic TypeError message sent to stdout - "TypeError:
too many arguments; expected 1, got 2" - no actual exception and no
indication where it occurs.)

You're not using the mandatory 'event' parameter that is passed in -

  def OnShadowOn(self, event):

Regards,
Denny

···

On Wed, 28 Feb 2001, Newhard, Nick wrote:

----------------------------------------------------------
Denny De La Haye \ www-edc.eng.cam.ac.uk/~djd33
Engineering Design Centre, \ www-edc.eng.cam.ac.uk
Department of Engineering, \ www.eng.cam.ac.uk
University of Cambridge, UK \ www.cam.ac.uk
----------------------------------------------------------
"The more you read, the less everyone else knows"

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users