📌  相关文章
📜  wxPython - wx.TreeCtrl 中的 GetBoundingRect() 方法

📅  最后修改于: 2022-05-13 01:55:23.888000             🧑  作者: Mango

wxPython - wx.TreeCtrl 中的 GetBoundingRect() 方法

在本文中,我们将学习 wx.TreeCtrl 中的 GetBoundingRect() 方法。 GetBoundingRect() 返回包围项目的矩形。如果 textOnly 为 True,则仅返回项目标签周围的矩形,否则,项目的图像也会被考虑在内。如果矩形未成功检索,则返回值可能为 None,例如该项目当前不可见。

GetBoundingRect() 接受两个参数 item 和 textOnly。

参数:

ParameterTypeDescription
itemwx.TreeItemId                      item that we want to ensure to be visible.
textOnly                        boolean If textOnly is True, only the rectangle around the item’s label will be returned, otherwise the item’s image is also taken into account.

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") 
        item3 = self.tree.AppendItem(item, "SubItem")
        item4 = self.tree.AppendItem(item, "SubItem")
        item5 = self.tree.AppendItem(item2, "SubItem")
        item6 = self.tree.AppendItem(item, "SubItem")
  
        # print bound rectangle pyObject
        print(self.tree.GetBoundingRect(item, False))
  
        # expand all nodes of the tree
        self.tree.ExpandAllChildren(item) 
          
        sizer = wx.BoxSizer(wx.VERTICAL) 
        sizer.Add(self.tree, 0, 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()


输出:

(0, 0 , 10, 10)