wxpythonparameter-passingwxwidgetsmultibindingwxnotebook

wxPython : Notebook seems not to work with multibinding


I am trying to create a wxPython app. Simplifying my setup, let's say that I have a left and a right panel. Left panel has my controls, which are sliders/buttons/etc. Right panel includes a notebook, where in each tab I am displaying video frames, taken from an image data list. If I do not use the notebook, everything is fine, controls events alter the function of the right panel. However, when I implement the notebook, where a multibinding for each panel inside the notebook and each control is considered, I get some strange behavior. What happens is that every control seems to have a different parent, thus I can not pass variables inside the same class(!!!) from one method to another. Due to the complexity of the issue I am not able to interpret the results. The following is an example:

import wx
import numpy as np


def checkclass(obj, clas):
    if isinstance(obj, clas) or issubclass(obj.__class__, clas):
        return 1
    else:
        return 0



def wx_generic_binder(widget, function):
    '''
    TextCtrl(wx.EVT_TEXT) and Slider(wx.EVT_SLIDER) are supported for now.
    '''
    if widget is not None:
        if checkclass(widget, wx.TextCtrl):
            widget.Bind(wx.EVT_TEXT, function)
        elif checkclass(widget, wx.Slider):
            widget.Bind(wx.EVT_SLIDER, function)
        else:
            raise NotImplementedError
class TopicsNotebook(wx.Notebook):
    def __init__(self, parent, forced_frame_handler):
        wx.Notebook.__init__(self, parent)
        self.pages = []
        for count in range(3):
            self.pages.append(VideoPanel(self,forced_frame_handler))
            self.AddPage(self.pages[-1], str(count))

class VideoPanel(wx.Panel):
    '''
    A video panel implementation
    '''

    def __init__(self, parent,  forced_frame_handler):
        '''
        data is a list of frames. If a frame is missing,
        the entry is None
        '''
        wx.Panel.__init__(self, parent, wx.NewId())
        self.forced_frame_handler = forced_frame_handler
        wx_generic_binder(self.forced_frame_handler,
                          lambda event: self.handle_forced(event, self)
                          )
        self.forced = 0
        print 'from __init__', id(self)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.Bind(wx.EVT_PAINT, self.on_playing)
        wx.CallLater(200, self.SetFocus)
        self.img = None


    def handle_forced(self, event, parent):
        self.count = self.forced_frame_handler.GetValue()
        self.forced = 1
        print 'from handle_forced', id(self)
        self.on_playing(None)
    def on_playing(self, event):
        print 'from on_playing', id(self)


class MainFrame(wx.Frame):
    '''
    Main Processing Window
    '''

    def __init__(self, parent, id_, title):

        wx.Frame.__init__(self, parent, id_, title)
        self.main_panel = wx.Panel(self, wx.NewId())
        self.lft_box = wx.BoxSizer(wx.VERTICAL)
        self.slider_min = wx.Slider(self.main_panel, -1, 0, 0,
                                    99, size=(600, -1),
                                    style=wx.SL_VALUE_LABEL)
        self.lft_box.Add(self.slider_min)

        self.rgt_box = wx.BoxSizer(wx.VERTICAL)

        self.nb = TopicsNotebook(self, forced_frame_handler=self.slider_min)
        self.rgt_box.Add(self.nb, 1, wx.EXPAND | wx.ALL)
        self.main_panel.Fit()
        self.main_box = wx.BoxSizer(wx.HORIZONTAL)
        self.main_box.AddMany([(self.lft_box, 1),
                               (self.rgt_box, 1)])
        self.SetSizerAndFit(self.main_box)
        wx.CallLater(200, self.SetFocus)

def main():
    '''
    main function
    '''
    app = wx.App(0)

    frame = MainFrame(None , -1, 'Data Mining')
    frame.Show(True)
    app.MainLoop()

main()

which gives as a result: (if someone moves the slider)

from __init__ 139699098836624
from __init__ 139699098836016
from __init__ 139699098624232
from on_playing 139699098836624
from on_playing 139699098836624
from on_playing 139699098836624
from handle_forced 139699098624232
from on_playing 139699098624232
from handle_forced 139699098624232
from on_playing 139699098624232
from handle_forced 139699098624232
from on_playing 139699098624232
from handle_forced 139699098624232
from on_playing 139699098624232
from handle_forced 139699098624232
from on_playing 139699098624232

One can see that the on_playing has the same id for all the times called(??), not obeying to init ids. The handle_forced is calling the occurences after the three first calls of one_playing, that's why I am getting the same id. It would be normal to get 3 such occurences from 3 different handle_forced instances, but I am getting only the last one. All in all, the ids are messed up, with only one(random?) binding for each handler to be set. Anyone with enough patience is welcome to give me an explanation. Thank you!


Solution

  • You are basically binding the same event type to the same widget 3 times. When the event happens, the system looks for a binding, calls the first one it finds, and then assumes it is done and returns. If you want the system to continue looking for matching bindings then you need to call event.Skip() in the handler function.