Tkinter inserts Combobox inside Treeview widget

For example, you can 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') 

Fill in the root, insert the children as usual. At the end of the block, you can see where I want to put the Combobox in the tree using the 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 ... 

The following is a definition of the Combo class. As I understand it, the combobox widget is inherited and should 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 be able to switch between units (e.g. m / s, ft / s, etc.) from the Treeview widget.

Any suggestions, plz?

what i want

+4
source share
2 answers

The treeview widget does not support embedded widgets. Values ​​for the values attribute are treated as strings.

+4
source

By default, Treeview is a static display of a string list forest. However, when working, having carefully read the Treeview links, you can make Treeview quite interactive. On this subject, I would attach a left click to an event handler that compares the x, y mouse with a bounding box (.bbox) for the unit attribute cell. If a Combobox initialized with the current value (for example, "flops") is displayed in the field, immediately above the unit attribute cell.

Tkinter.ttk Link to Treeview and Link to tcl / tk treeview

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

0
source

Source: https://habr.com/ru/post/1482771/


All Articles