当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


wxPython wx.TreeCtrl GetFirstVisibleItem()用法及代码示例


在本文中,我们将讨论与wxPython的wx.TreeCtrl类关联的GetFirstVisibleItem()函数。让我们看看它的用途和用途:

用法:wx.TreeCtrl.GetFirstVisibleItemId(self)

参数:

GetFirstVisibleItemId()方法不需要任何参数。

返回值:



wx.TreeItemId对象,它是树的第一个可见项

其实现如下:

如果第一个可见项目有效或无效,则打印以下示例。我们将使用与wx.TreeItemId对象IsOk()方法关联的IsOk()方法执行此操作,如果树项目是有效项目,则该方法返回true。

范例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()

输出:

Is Valid Item

范例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()

输出:

Invalid Tree Item




相关用法


注:本文由纯净天空筛选整理自RahulSabharwal大神的英文原创作品 wxPython – GetFirstVisibleItem() method in wx.TreeCtrl。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。