wx.GenericDirCtrl事件的处理

时间:2010-10-31 23:27:02

标签: python wxpython

我正在使用此控件,但我无法处理控件点击(和其他事件)。 这是我的代码:

class BoExplorerPanel(wx.Panel):
  def __init__(self, parent):
     wx.Panel.__init__(self, parent, wx.ID_ANY)

     self.initComponents() # initialize Window components

  def initComponents(self):
     print "Inizializzo i controlli"
     # controls
     resizeBox = wx.BoxSizer(wx.VERTICAL)

     self.dirBrowser = wx.GenericDirCtrl(self, wx.ID_ANY, style = wx.DIRCTRL_DIR_ONLY)

     resizeBox.Add(self.dirBrowser, 1, wx.EXPAND | wx.ALL)

     self.SetSizerAndFit(resizeBox)

     # events
     self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.dirBrowser_OnItemSelected,   self.dirBrowser)
     self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.dirBrowser_OnRightClick, self.dirBrowser)
     self.Bind(wx.EVT_TREE_SEL_CHANGED, self.dirBrowser_OnSelectionChanged, self.dirBrowser)

     # panel's properties

  def dirBrowser_OnItemSelected(self, event):
     print "CLicked"

  def dirBrowser_OnRightClick(self, event):
     print "Right Click"

  def dirBrowser_OnSelectionChanged(self, event):
     print "Selection Changed"

1 个答案:

答案 0 :(得分:2)

您需要绑定到目录类的TreeCtrl,而不是该类本身。

下面的固定代码。请注意事件处理程序中对event.Skip()的调用(注释掉它以查看其效果)

#!/usr/bin/python
import wx

class BoExplorerPanel(wx.Frame):
  def __init__(self):
     wx.Frame.__init__(self, None)

     self.initComponents() # initialize Window components

  def initComponents(self):
     print "Inizializzo i controlli"
     # controls
     resizeBox = wx.BoxSizer(wx.VERTICAL)

     self.dirBrowser = wx.GenericDirCtrl(self, wx.ID_ANY, style = wx.DIRCTRL_DIR_ONLY)

     resizeBox.Add(self.dirBrowser, 1, wx.EXPAND | wx.ALL)

     self.SetSizerAndFit(resizeBox)

     # events
     tree = self.dirBrowser.GetTreeCtrl()
     self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.dirBrowser_OnItemSelected, tree)
     self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.dirBrowser_OnRightClick, tree)
     self.Bind(wx.EVT_TREE_SEL_CHANGED, self.dirBrowser_OnSelectionChanged, tree)

     # panel's properties

  def dirBrowser_OnItemSelected(self, event):
     print "CLicked"
     event.Skip()

  def dirBrowser_OnRightClick(self, event):
     print "Right Click"
     event.Skip()

  def dirBrowser_OnSelectionChanged(self, event):
     print "Selection Changed"
     event.Skip()


app = wx.App(False)
f = BoExplorerPanel()
f.Show()
app.MainLoop