简体   繁体   中英

wxPython: Check if tree node has focus (TreeCtrl)

Do not be intimidated by the code. It is really straightforward. (Run it if you want)

I am trying to figure out a way to do something when a tree node has focus. I know if I want to do something if the TreeCtrl has focus then it would be:

self.tree.Bind(wx.EVT_SET_FOCUS, some_function)

But I don't want that, I want it for tree node. Any help?

For my application, a pop up window would show up for every tree node selected, and disappears if the tree node loses focus.

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(600, 400))
        self.splitter = wx.SplitterWindow(self, -1)
        self.leftPanel = wx.Panel(self.splitter, -1)
        self.tree = MyTree(self.leftPanel, 1, wx.DefaultPosition, (300, 300))
        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)

        self.rightPanel = wx.Panel(self.splitter, -1)

        self.splitter.SplitVertically(self.leftPanel, self.rightPanel)

        sizer = wx.BoxSizer ( )
        sizer.Add ( self.splitter, 1, wx.EXPAND )
        self.SetSizer ( sizer )

        self.tree.SetFocus()

    def OnSelChanged(self, event):
        print 'Sel Changed'


class MyTree(wx.TreeCtrl):
    def __init__(self, panel, id, pos, size):
        self.panel = panel
        wx.TreeCtrl.__init__(self, panel, id, pos=pos, size=size, style=wx.TR_HAS_BUTTONS)

        wx.EVT_TREE_ITEM_EXPANDING(self,-1,self.OnItemExpanding)
        wx.EVT_TREE_ITEM_COLLAPSED(self,-1,self.OnItemCollapsed)
        wx.EVT_SET_FOCUS(self,self.OnGotFocus)

        self.root = self.AddRoot('Root')
        self.SetPyData(self.root,0)
        self.SetItemHasChildren(self.root)
        self.Expand(self.root)
        self.SetImageList(wx.ImageList(16,16))

    def OnItemExpanding(self,evt):
        node = evt.GetItem()
        data = self.GetPyData(node)
        if data:
            for i in range(1,2):
                leaf = self.AppendItem(node,'%s.%s' % (data,i))
                self.SetPyData(leaf,'%s.%s' % (data,i))
        else:
            for i in range(1,2):
                leaf = self.AppendItem(node,'%s' % (i))
                self.SetPyData(leaf,'%s' % (i))
                self.SetItemHasChildren(leaf)

    def OnItemCollapsed(self,evt):
        self.DeleteChildren(evt.GetItem())

    def OnGotFocus(self,evt):
        print 'tree got focus'
        evt.Skip()

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "This is a test")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()

I believe you want

...
    self.tree.Bind(wx.EVT_TREE_SEL_CHANGED,self.OnTreeSelectionChange)
    self.tree.Bind(wx.EVT_KILL_FOCUS,self.OnKillTreeFocus)

...
def OnTreeSelectionChange(self, evt):
    print "OnSelChanged:   ", self.GetItemText(evt.GetItem())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM