python treeview列“stretch=False”不工作

2024-10-03 04:32:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想禁用列大小调整,但是“stretch=False”不起作用,我不知道为什么,我的python3.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()

Tags: textfromimportfalsemaintkintercolumnanchor
2条回答

尝试在mainloop()之前添加此项

gMaster.resizable(0,0)

你不需要拉伸=假

下面是一个带注释的演示。尝试改变拉伸的值和改变应用程序窗口的宽度,你会看到区别。也许没有必要阻止用户调整列的大小。相反,更重要的是给每个列一个适当的初始宽度,以便可以轻松地显示其内容。在

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

相关问题 更多 >