5

I have a pretty basic Treeview set up in Python:

self.tv = ttk.Treeview(top_frame, columns=("#","ID","Name"), selectmode = "browse" )

self.tv.heading('#1', text='#', anchor=W)
self.tv.heading('#2', text='ID', anchor=W)
self.tv.heading('#3', text='Name', anchor=W)

self.tv.column('#1',minwidth=70, width = 70, stretch=NO)
self.tv.column('#2', minwidth = 240, width = 240, stretch=NO)
self.tv.column('#3', minwidth=260, width = 260, stretch=NO)
self.tv.column('#0', minwidth=0, width=0, stretch=NO)

The problem I have is that the columns can be resized to make the treeview either wider than its container, or much more narrow. Both of which ruin the aesthetics of the whole thing.

From what I've read, stretch = NO should be disabling this, but it is not. I'm testing the GUI on a Mac with Python 2.7.9. I know certain widgets just plain don't work 100% on a Mac, so am I doing something wrong, or is that all I can expect?

nbro
  • 15,395
  • 32
  • 113
  • 196
user1220109
  • 131
  • 1
  • 3
  • 9
  • 1
    (For future readers) It's possible to catch and block the `` event when it's over a treeview separator as I outline [in this answer](https://stackoverflow.com/a/46120502/736937) to an apparently duplicate question. – jedwards Sep 08 '17 at 16:05

2 Answers2

1

To disable column resizing you need to create a def to break the click when it is in the separator x and y. See the example:

def handle_click(event):
    if treeview.identify_region(event.x, event.y) == "separator":
        return "break"
    
    #...


treeview.bind('<Button-1>', handle_click)

I hope I can help everybody who needs to solve it.

j_4321
  • 15,431
  • 3
  • 34
  • 61
0

column()'s stretch parameter takes True/False in Python ttk. "stretch=False" works for me on OSX. See https://docs.python.org/2/library/ttk.html#ttk.Treeview.column

  • Can't test on a Mac, but this shouldn't fix the issue, because the option is only relevant when the treeview is resized, not when the user resizes columns. The linked docs even say so. – ProblemsLoop Jul 01 '22 at 08:07