wxPython:Notebook似乎无法使用多重绑定

时间:2017-01-31 20:21:30

标签: wxpython parameter-passing wxwidgets multibinding wxnotebook

我正在尝试创建一个wxPython应用程序。简化我的设置,让我们说我有一个左侧和右侧面板。左侧面板有我的控件,它们是滑块/按钮/等。右侧面板包括一个笔记本,我在每个标签中显示视频帧,从图像数据列表中获取。如果我不使用笔记本电脑,一切都很好,控制事件改变了右面板的功能。但是,当我实现笔记本时,笔记本中每个面板的多重绑定和每个控件被考虑,我得到一些奇怪的行为。会发生的是每个控件似乎都有不同的父级,因此我无法将同一个类(!!!)中的变量从一个方法传递到另一个方法。由于问题的复杂性,我无法解释结果。以下是一个例子:

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()
结果给出了

:(如果有人移动滑块)

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

可以看出on_playing在所有被称为(??)的时间内具有相同的id,而不是服从 init id。 handle_forced在one_playing的三个第一次调用之后调用出现的事件,这就是为什么我得到相同的id。从3个不同的handle_forced实例中获取3个这样的事件是正常的,但我只得到最后一个。总而言之,这些ID混乱了,每个处理程序只有一个(随机?)绑定。任何有足够耐心的人都欢迎给我一个解释。谢谢!

1 个答案:

答案 0 :(得分:1)

您基本上将相同的事件类型绑定到同一个窗口小部件3次。当事件发生时,系统会查找绑定,调用它找到的第一个绑定,然后假定它已完成并返回。如果您希望系统继续查找匹配的绑定,那么您需要在处理函数中调用event.Skip()