7

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

0

2 Answers 2

9

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

2
  • BTW would you be able to suggest anything to me so that I can accomplish my goal of switching units in the treeview? Basically I want the user to be able to decide whether the program should display m/s, knots, mph, etc...
    – ADB
    Commented May 26, 2013 at 20:27
  • @ADB: you might want to look at this: tkintertreectrl.sourceforge.net I haven't used it myself, but I've used tktreectrl with tcl/tk aps and it certain has a lot of features. Commented May 26, 2013 at 20:38
3

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.

EDIT: The overlay answer above assumed a static treeview and a single cell to be overlayed. To be more general, use identify_row to see if an item was clicked. If so and it needs an overlay, use identify_column to see if the appropriate column was clicked. If so, use bbox(item, col) to determine where to place the overlay. The overlay need not be a combobox.

Or, consider a separate widget in the frame suggested above or a popup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.