Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

wxPython – GetFocusedItem() method in wx.TreeCtrl

  • Last Updated : 26 Nov, 2020

In this article, we are going to learn about GetFocusedItem() method associated with wx.TreeCtrl class of wxPython module. The GetFocusedItem() function is used in order to return the focused item. It returns the item last clicked or otherwise selected. No parameters are required by GetFocusedItem() method.

Unlike GetSelection(), it can be used whether the control has the TR_MULTIPLE style.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course



Return Type: wx.TreeItemId

Parameters: No parameters

In the below program we will return if the item is valid or not. Also, the wx.TreeItemId object is returned.


# import required modules
import wx
class MyTree(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
class TreePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
    # create tree control in window
        self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
        # create tree root
        self.root = self.tree.AddRoot('root')
        self.tree.SetPyData(self.root, ('key', 'value'))
        # add item to root
        item = self.tree.AppendItem(self.root, "Item")
        item2 = self.tree.AppendItem(self.root, "Item")
        # set focused/selected item in control
        # print if first visible item is
        # a valid tree item
            print("Valid Item")
            print("Invalid Item")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 100, wx.EXPAND)
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='TreeCtrl Demo')
        panel = TreePanel(self)
# Driver Code
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainFrame()


Valid Item
<wx._core.TreeItemId object at 0x000001DAED4141F8>

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!