Question

For example, lets create a Treeview widget using a class as follows:

class FiltersTree:
    def __init__(self, master, filters):
        self.master = master
        self.filters = filters
        self.treeFrame = Frame(self.master)
        self.treeFrame.pack()
        self._create_treeview()
        self._populate_root()

    def _create_treeview(self):
        self.dataCols = ['filter', 'attribute']
        self.tree = ttk.Treeview(self.master, columns = self.dataCols, displaycolumns = '#all')

Populate root, insert children as usual. At the end of the codeblock, you can see where I want to put a Combobox in the tree, using a Combo object:

    def _populate_root(self):
        # a Filter object
        for filter in self.filters:
            top_node = self.tree.insert('', 'end', text=filter.name)

            # a Field object
            for field in filter.fields:
                mid_node = self.tree.insert(top_node, 'end', text = field.name)

                # insert field attributes
                self.insert_children(mid_node, field)

    def insert_children(self, parent, field):
        name = self.tree.insert(parent, 'end', text = 'Field name:',
                         values = [field.name])
        self.tree.insert(parent, 'end', text = 'Velocity: ', 
                         values = [Combo(self)]) # <--- Combo object
        ...

Next the class definition of Combo follows. The way I understand it, the combobox widget inherits from and must be placed inside the Labelframe widget from ttk:

class Combo(ttk.Frame):
    def __init__(self, master):
        self.opts = ('opt1', 'opt2', 'etc')
        self.comboFrame = ttk.Labelframe(master, text = 'Choose option')
        self.comboFrame.pack()
        self.combo = ttk.Combobox(comboFrame, values=self.opts, state='readonly')
        self.combo.current(1)
        self.combo.pack()

So is this completely wrong? I want to have the ability to change between units (eg m/s, ft/s, etc) from within the Treeview widget.

Any suggestions, plz?

what i want

Was it helpful?

Solution

The treeview widget doesn't support embedded widgets. The values for the values attribute are treated as strings.

OTHER TIPS

By default, a Treeview is a static display of a forest of lists of strings. However, with work, after carefully reading Treeview references, one can make a Treeview fairly interactive. For this question, I would bind left click to an event handler that compares the mouse x,y to the bounding box (.bbox) for the units attribute cell. If in the box, display a Combobox, initialized with the current value (such as 'flops'), directly on top of the units attribute cell.

Tkinter.ttk Treeview reference and Tcl/tk treeview reference

Of course, it might be easier to put the Treeview in a frame with with a separate Combobox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top