Wednesday, July 3, 2024
HomeLanguagesPythonwxPython – GetFirstVisibleItem() method in wx.TreeCtrl

wxPython – GetFirstVisibleItem() method in wx.TreeCtrl

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.GetFirstVisibleItem(self)

Parameters:

No parameters are required by GetFirstVisibleItem() 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

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments