wx.GenericDirCtrl事件处理

2024-06-25 23:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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"

Tags: selfeventidtreeinitbinddefdirbrowser
1条回答
网友
1楼 · 发布于 2024-06-25 23:08:11

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

修正了下面的代码。注意呼叫事件。跳过()在事件处理程序中(将其注释掉以查看其效果)

#!/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

相关问题 更多 >