Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

python 3.x - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...