wxPython - wx.TreeCtrl 中的 GetFocusedItem() 方法
在本文中,我们将学习与wxPython模块的wx.TreeCtrl类关联的GetFocusedItem()方法。 GetFocusedItem()函数用于返回焦点项目。它返回上次单击或以其他方式选择的项目。 GetFocusedItem()方法不需要参数。
与GetSelection()不同,它可以用于控件是否具有TR_MULTIPLE样式。
句法:
wx.TreeCtrl.GetFocusedItem(self)
Return Type: wx.TreeItemId
Parameters: No parameters
在下面的程序中,我们将返回该项目是否有效。此外,还返回wx.TreeItemId对象。
Python
# 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,
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")
# set focused/selected item in control
self.tree.SetFocusedItem(item)
# print if first visible item is
# a valid tree item
if(self.tree.GetFocusedItem().IsOk()):
print("Valid Item")
print(self.tree.GetFocusedItem())
else:
print("Invalid 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()
# Driver Code
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()
输出:
Valid Item