0

I want to disable column resize, but "stretch = False" is not working, I don't know why, my python version 3.4.3 .

from tkinter import *
from tkinter import ttk

def main():
    gMaster = Tk()
    w = ttk.Treeview(gMaster, show="headings", columns=('Column1', 'Column2'))
    w.heading('#1', text='Column1', anchor=W)
    w.heading('#2', text='Column2', anchor=W)

    w.column('#1', minwidth = 70, width = 70, stretch = False)
    w.column('#2', minwidth = 70, width = 70, stretch = False)
    w.grid(row = 0, column = 0)
    mainloop()

if __name__ == "__main__":  
    main()
Pedru
  • 1,430
  • 1
  • 14
  • 32
葉家和
  • 3
  • 1
  • 3
  • You misunderstood the meaning of stretch, in Python3 doc, 25.2.9.6 ttk.Treeview, it says: `stretch: True/False. Specifies whether the column’s width should be adjusted when the widget is resized.` It means if you set stretch of a column to False, the column's width will not change automatically as you adjust the width of the Treeview. But you still can adjust the width of the column manually. – etuc Apr 11 '16 at 12:21
  • Well, I try to understand. Is there any method to achieve disable column resize? – 葉家和 Apr 11 '16 at 17:10
  • I don't think it's necessary to disable column resize. A comment is not enough for demo code, so I write an answer bellow to help you understand the meaning of stretch. – etuc Apr 12 '16 at 00:46
  • 2
    (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:06

3 Answers3

3

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. Sorry for the bad English, it's not my first language.

2

Here is a demo with comments. Try to change the value of stretch and change the width of the application window, you will see the difference. Maybe there is no need to keep users from resizing a column. Instead, it is more important to give each column a proper initial width so that its content can be shown comfortably.

from tkinter import *
from tkinter import ttk

def main():
    gMaster = Tk()
    w = ttk.Treeview(gMaster, show="headings", columns=('Column1', 'Column2'))
    w.heading('#1', text='Column1', anchor=W)
    w.heading('#2', text='Column2', anchor=W)

    w.column('#1', minwidth = 70, width = 70, stretch = False)
    w.column('#2', minwidth = 70, width = 70, stretch = True)  # Try to change the value of stretch here.

    # The following 2 lines will make the Treeview `w` fill the window horizontally.
    w.grid(row = 0, column = 0, sticky='we')
    gMaster.grid_columnconfigure(0, weight=1)

    mainloop()

if __name__ == "__main__":
    # Try to change the width of the application window use your mouse and you will see
    # the width of column #2 will:
    # 1. keep unchanged when strech=False
    # 2. change when strech=True
    main()
etuc
  • 96
  • 5
0

Try adding this before mainloop()

gMaster.resizable(0,0)

You don't need stretch = False

Oditi
  • 23
  • 5