Help debugging bind error

Hello,

I am trying to learn how to us pyOpenGL with wxpython. By the way, hats off to the wxPython team it is a really great GUI dev platform. Been using it for probably 8 years. I really like it and feel it is been very reliable and a fantastic experience! I mainly wanted to say that because this is my first post.

Anyways, I have a binding problem and I’m unsure why. I’m using Python 2.7 and am trying to run the attached example. Ex2.py (3.4 KB)

I’m trying to use the wx.EvtHandler.Bind call instead of wx.EVT_PAINT because the latter is deprecated. I’m confused because the error I get shown below:

TypeError: unbound method _EvtHandler_Bind() must be called with EvtHandler instance as first argument (got PyEventBinder instance instead)

implies that the first argument shouldn’t be the wx.EVT_PAINT but when I ctrl click on the .Bind function it shows the following method prameters:

        Bind an event to an event handler.
            
            :param event: One of the ``EVT_*`` event binder objects that
                          specifies the type of event to bind.
            
            :param handler: A callable object to be invoked when the
                            event is delivered to self.  Pass ``None`` to
                            disconnect an event handler.
            
            :param source: Sometimes the event originates from a
                           different window than self, but you still
                           want to catch it in self.  (For example, a
                           button event delivered to a frame.)  By
                           passing the source of the event, the event
                           handling system is able to differentiate
                           between the same event type from different
                           controls.
            
            :param id: Used to spcify the event source by ID instead
                       of instance.
            
            :param id2: Used when it is desirable to bind a handler
                        to a range of IDs, such as with EVT_MENU_RANGE.

When I’ve used this bind before I’ve done the following: (just an example)

self.Bind(wx.EVT_MENU, self.onGainSetBase10, id=211)

So in this example, I’m unsure how to rebind these events to handlers to prevent the deprecation issue from the original bindings.

self.Bind(wx.EVT_MENU, self.onGainSetBase10, id=211)

self.Bind(wx.EVT_PAINT, self.OnDraw)

should work.

Scott

Hi Scott,

Thank you that worked! I’m new to this kind of higher level software. How did you know the GLCanvas class had a Bind function?

Rob,

Look at the inheritance diagram in the docs.

GLCanvas: subclass of wx.Window
wx.Window: subclass of wx.EvtHandler

The latter contains the Bind method and is inherited by GLCanvas via wx.Window.

More info here at e.g. RealPython.

Slaînte,
Thom

Because they all do. :slight_smile: Well, all classes that handle events do. It’s a
little complicated to find in the documentation, but if you look at the
documentation for GLCanvas [1], you can see that wx.glcanvas.GLCanvas
inherits from wx.Window, which inherits from wx.EvtHandler. wx.EvtHandler
is where the Bind() method comes from. But as a rule of thumb, any class
that handles events will have a Bind() method and you can set up event
handlers this way.

Scott

[1] https://docs.wxpython.org/wx.glcanvas.GLCanvas.html#class-hierarchy-class-hierarchy

Whoops. Looks like Thom beat me to it. Guess I should read all my emails before replying. :slight_smile:

Didn’t beat you… look at it this way, Scott:
the “advantage” of another wx.Locale😏

Thank you guys! This level of software is a bit overwhelming to an embedded guy and I have a hard time tracking some of this down. My IDE was showing that Bind call as an error for some reason, but the code ran fine.

Thanks again!

Hi Thom,
I"m using pycharm.

Also - is there anything for me to do like “accept solution” or “close thread” or something to mark this as solved?

Rob,

PyCharm is a very professional editor. It surprises me it gave you “an error for some reason” on the Bind call. I would consult its documentation or google on that. Impossible to tell without the error, though.

@Robin can surely answer your last question.

FYI, there is a Discourse solved-accepted-answer-plugin, so technically this is an option if it’s implemented in this forum.

Thom

I didn’t install that plugin when setting this up this site. Although it lets people skip to the answer, usually there is lots of good content in the discussion leading up to the answer that would be valuable for the reader as well.

If people want something like this they can always edit their original post and add a link to the message with the answer.

1 Like