Skip to content
Related Articles

Related Articles

Improve Article

wxPython – GetFirstVisibleItem() method in wx.TreeCtrl

  • Last Updated : 26 Nov, 2020

In this article we are going to discuss GetFirstVisibleItem() function associated with wx.TreeCtrl class of wxPython. Lets look at how and what is it used for:

Syntax: wx.TreeCtrl.GetFirstVisibleItemId(self)

 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

Parameters:



No parameters are required by GetFirstVisibleItemId() method.

Returns:

wx.TreeItemId object that is the first visible item of the tree

Its implementation is given below:

The following examples print if the first visible item is valid or invalid. We will perform this using the IsOk() method associated with wx.TreeItemId object IsOk() method which returns true if tree item is a valid item.

Example 1:

Python




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,
                           wx.TR_HAS_BUTTONS)
  
        # 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")
  
        # print if first visible item is a valid tree item
        if(self.tree.GetFirstVisibleItem().IsOk()):
            print("Is Valid Item")
        else:
            print("Invalid Tree Item")
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 100, wx.EXPAND)
        self.SetSizer(sizer)
  
  
class MainFrame(wx.Frame):
  
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='TreeCtrl Demo')
        panel = TreePanel(self)
        self.Show()
  
  
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainFrame()
    app.MainLoop()

Output:

Is Valid Item

Example 2:

Python




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,
                           wx.TR_HAS_BUTTONS)
  
        # print if first visible item is a valid tree item
        if(self.tree.GetFirstVisibleItem().IsOk()):
            print("Is Valid Item")
        else:
            print("Invalid Tree Item")
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 100, wx.EXPAND)
        self.SetSizer(sizer)
  
  
class MainFrame(wx.Frame):
  
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='TreeCtrl Demo')
        panel = TreePanel(self)
        self.Show()
  
  
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainFrame()
    app.MainLoop()

Output:

Invalid Tree Item




My Personal Notes arrow_drop_up
Recommended Articles
Page :