Web Analytics Made Easy - Statcounter
Skip to content

Tree

Tree call reference

The Tree Element and Table Element are close cousins. Many of the parameters found in the Table Element apply to Tree Elements. In particular the heading information, column widths, etc..

Unlike the Table element the Tree element uses a "helper class" that represents the tree data structure. A "table" is a simple 2 dimensional array, but a tree is a bit more complex, thus an additional data structure is needed so that you can build your tree and then pass it to the Tree element.

The order of operations to make a tree is to take these steps:

  • Get a TreeData Object
  • "Insert" data into the tree
  • Pass the filled in TreeData object to Tree Element
treedata = sg.TreeData()

treedata.Insert("", '_A_', 'A', [1, 2, 3])
treedata.Insert("", '_B_', 'B', [4, 5, 6])
treedata.Insert("_A_", '_A1_', 'A1', ['can', 'be', 'anything'])

layout = [[sg.Tree(treedata, headings=["X", "Y", "Z"])]]

Events

The Tree element will return an event when an item is selected if the enable_events parameter is set to True

Values Dictionary

List of the currently selected items. These are the keys from the TreeData class that was used to create/update the tree..

Demo Programs

One of the best way to learn about the complex elements is to use the Demo Programs, Cookbook and other additional materials.

TreeData format

def TreeData()
def Insert(self, parent, key, text, values, icon=None)

To "insert" data into the tree the TreeData method Insert is called.

Insert(parent_key, key, display_text, values)

To indicate insertion at the head of the tree, use a parent key of "". So, every top-level node in the tree will have a parent node = ""

This code creates a TreeData object and populates with 3 values

treedata = sg.TreeData()

treedata.Insert("", '_A_', 'A', [1,2,3])
treedata.Insert("", '_B_', 'B', [4,5,6])
treedata.Insert("_A_", '_A1_', 'A1', ['can','be','anything'])

Note that you can use the same values for display_text and keys. The only thing you have to watch for is that you cannot repeat keys.

When reading a window the Table Element will return a list of rows that are selected by the user. The list will be empty if no rows are selected.

Icons on Tree Entries

If you wish to show an icon next to a tree item, then you specify the icon in the call to Insert. You pass in a filename or a Base64 bytes string using the optional icon parameter.

Here is the result of showing an icon with a tree entry.

image