wxPython - wx.TreeCtrl 中的 GetFirstVisibleItem() 方法
在本文中,我们将讨论与 wxPython 的 wx.TreeCtrl 类关联的 GetFirstVisibleItem()函数。让我们看看它的用途和用途:
Syntax: wx.TreeCtrl.GetFirstVisibleItemId(self)
Parameters:
No parameters are required by GetFirstVisibleItemId() method.
Returns:
wx.TreeItemId object that is the first visible item of the tree
其实现如下:
以下示例打印第一个可见项是否有效。我们将使用与 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()
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()
输出:
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