python treeview列" stretch = False"不工作

时间:2016-04-11 11:17:02

标签: python tkinter

我想禁用列调整大小,但是" stretch = False"不工作,我不知道为什么,我的python版本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()

2 个答案:

答案 0 :(得分:2)

这是一个带注释的演示。尝试更改拉伸值并更改应用程序窗口的宽度,您将看到差异。也许没有必要让用户不要调整列的大小。相反,更重要的是为每列提供适当的初始宽度,以便可以舒适地显示其内容。

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()

答案 1 :(得分:0)

要禁用列调整大小,您需要创建一个 def 以在它位于分隔符 x 和 y 中时中断单击。看例子:

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

#...

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

希望能帮到大家需要解决的问题。 抱歉英语不好,这不是我的母语。