Python 树视图列"stretch=False"不起作用



我想禁用列大小调整,但"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()

要禁用列大小调整,您需要创建一个def来在分隔符x和y中打断点击。参见示例:

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

我希望我能帮助每一个需要解决的人。抱歉英语不好,这不是我的第一语言。

这是一个带有注释的演示。尝试更改拉伸的值和应用程序窗口的宽度,你会看到差异。也许没有必要阻止用户调整列的大小。相反,更重要的是给每列一个合适的初始宽度,这样它的内容就可以轻松地显示出来。

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

尝试在mainloop()之前添加此

gMaster.resizable(0,0)

您不需要拉伸=错误

最新更新