Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter insert a Combobox inside a Treeview widget

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

like image 216
ADB Avatar asked May 26 '13 00:05

ADB


1 Answers

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

like image 92
Bryan Oakley Avatar answered Sep 19 '22 12:09

Bryan Oakley