wx.python将事件绑定到editableListBox列表项

时间:2015-11-04 22:57:45

标签: python python-2.7 wxpython wxwidgets

我正在寻找一种方法将函数绑定到从wx.gizmos editableListBox中选择列表项触发的事件。我希望选择用一个可以由用户编辑的字符串填充richTextCtrl1。

下面是一个快速模拟UI设计,没有事件绑定或功能,因为我的实际应用程序要大得多。

import wx
import wx.richtext
import wx.gizmos

def create(parent):
        return myFrame(parent)

[wxID_MYFRAME, wxID_MYFRAMEMYEITABLELISTBOX, wxID_MYFRAMEMYFRAME_PANEL, 
 wxID_MYFRAMERICHTEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(4)]

class myFrame(wx.Frame):
    def _init_ctrls(self, prnt):

        wx.Frame.__init__(self, id=wxID_MYFRAME, name=u'myFrame', parent=prnt,
          pos=wx.Point(551, 188), size=wx.Size(500, 500),
          style=wx.DEFAULT_FRAME_STYLE, title=u'myPanel')
        self.SetClientSize(wx.Size(498, 466))
        self.SetInitialSize(wx.Size(500, 500))

        self.myFrame_panel = wx.Panel(id=wxID_MYFRAMEMYFRAME_PANEL,
              name=u'myFrame_panel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(498, 466), style=wx.TAB_TRAVERSAL)

        self.myEitableListBox = wx.gizmos.EditableListBox(id=wxID_MYFRAMEMYEITABLELISTBOX,
              label=u'myEitableListBox', name=u'myEitableListBox',
              parent=self.myFrame_panel, pos=wx.Point(8, 56), size=wx.Size(200,
          248))
        self.myEitableListBox.SetStrings(['this', 'that', 'other'])

        self.richTextCtrl1 =    wx.richtext.RichTextCtrl(id=wxID_MYFRAMERICHTEXTCTRL1,
              parent=self.myFrame_panel, pos=wx.Point(224, 64),
              size=wx.Size(248, 240), style=wx.richtext.RE_MULTILINE,
              value=u'')
        self.richTextCtrl1.SetLabel(u'')

    def __init__(self, parent):
        self._init_ctrls(parent)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

我找不到一个查看wx.python演示示例的解决方案。谷歌也没有多少帮助。我试过Boa Constructor并将一些事件绑定到可编辑列表框。以下是我尝试过的绑定,但没有一个有效:

self.myEitableListBox.Bind(wx.EVT_SYS_COLOUR_CHANGED,
          self.OnmyEitableListBoxSysColourChanged)
self.myEitableListBox.Bind(wx.EVT_SET_FOCUS, self.OnmyEitableListBoxSetFocus)
self.myEitableListBox.Bind(wx.EVT_LEFT_DOWN, self.OnmyEitableListBoxLeftDown)

def OnmyEitableListBoxSysColourChanged(self, event):
    print self.myEitableListBox.GetSelection()
    print "fired"
    event.Skip()

def OnmyEitableListBoxSetFocus(self, event):
    print self.myEitableListBox.GetSelection(self)
    print "fired"
    event.Skip()

def OnmyEitableListBoxLeftDown(self, event):
    print self.myEitableListBox.GetSelection()
    lb = event.GetEventObject()
    data = lb.GetClientData(lb.GetSelection())
    print "fired", data
    event.Skip()

我一直在阅读文档并试图找到wx.python的tuts。我发现的任何内容都没有帮助我理解如何获得所选列表项。如果我可以获得所选列表项,我想我可以创建填充ritchTextFeild的函数。我不应该认为GetSelection()和我正在尝试并且无法理解发生了什么。此外,在运行时控制台中没有生成错误,所以我真的不知道从哪里开始。

谢谢

1 个答案:

答案 0 :(得分:0)

EditableListBox实际上包含一个ListCtrl,通过使用返回的对象绑定事件来获取对控件的使用 self.myEitableListBox.ListCtrl

以下添加了几行代码的示例将选择EditableListBox中的项目,并将其添加到richtext控件。

import wx
import wx.richtext
import wx.gizmos

def create(parent):
        return myFrame(parent)

[wxID_MYFRAME, wxID_MYFRAMEMYEITABLELISTBOX, wxID_MYFRAMEMYFRAME_PANEL, 
 wxID_MYFRAMERICHTEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(4)]

class myFrame(wx.Frame):
    def _init_ctrls(self, prnt):

        wx.Frame.__init__(self, id=wxID_MYFRAME, name=u'myFrame', parent=prnt,
          pos=wx.Point(551, 188), size=wx.Size(500, 500),
          style=wx.DEFAULT_FRAME_STYLE, title=u'myPanel')
        self.SetClientSize(wx.Size(498, 466))
        self.SetInitialSize(wx.Size(500, 500))

        self.myFrame_panel = wx.Panel(id=wxID_MYFRAMEMYFRAME_PANEL,
              name=u'myFrame_panel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(498, 466), style=wx.TAB_TRAVERSAL)

        self.myEitableListBox = wx.gizmos.EditableListBox(id=wxID_MYFRAMEMYEITABLELISTBOX,
              label=u'myEitableListBox', name=u'myEitableListBox',
              parent=self.myFrame_panel, pos=wx.Point(8, 56), size=wx.Size(200,
          248))
        self.myEitableListBox.SetStrings(['this', 'that', 'other'])

        self.richTextCtrl1 =    wx.richtext.RichTextCtrl(id=wxID_MYFRAMERICHTEXTCTRL1,
              parent=self.myFrame_panel, pos=wx.Point(224, 64),
              size=wx.Size(248, 240), style=wx.richtext.RE_MULTILINE,
              value=u'')
        self.richTextCtrl1.SetLabel(u'')

        self.myEitableListBox.ListCtrl.Bind(
            wx.EVT_LIST_ITEM_SELECTED, self.on_listbox)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def on_listbox(self, event):
        text = '{}\n'.format(event.Text)
        self.richTextCtrl1.AppendText(text)


if __name__ == '__main__':
    app = wx.App()
    frame = create(None)
    frame.Show()

    app.MainLoop()

由于使用了折旧的PySimpleApp,出现了错误,请使用App,我也在上面的代码中进行了更改。