How to disable manual resizing of Tkinter's Treeview column?

Viewed 4231

Since I can't horizontally scroll Treeview column due to what appears to be Tk/Tkinter limitation, I want to make it sticky so it is attached to the frame.

The issue is that user can manually resize Treeview column which can mess up my interface in a certain way. Is it possible to disable such functionality?

Note the size of column header.

Note the size of the column header.

User can drag mouse to resize column. I want to disable this.

User can drag mouse to resize column. I want to disable this.

Setting minwidth to a certain value prevents column from shrinking, but it is still possible to resize it to a larger width. I suppose I can react to changing width and just revert it to original, but there has to be a better way to do it.

5 Answers

I'll throw my hat in the ring - here's a dead-simple solution I found that prevents the user from resizing all columns in a ttk Treeview (named 'my_treeview' in this example):

my_treeview.bind('<Motion>', 'break')

HOW?

By binding a handler to the '<Motion>' event and setting it to 'break', hovering over column separators with the mouse no longer brings up the resize cursor (all mouse motion events are effectively disabled).

Treeview elements are still clickable and scrollable as usual, and setting column widths still works as expected.

Combining the answers from the other people:

def prevent_resize(event):
    if treeview.identify_region(event.x, event.y) == "separator":
        return "break"
treeview.bind('<Button-1>', prevent_resize)
treeview.bind('<Motion>', prevent_resize)

This blocks the resize, and also hides the "resize" mouse pointer

Related