【问题标题】:How to disable column resizing in a Treeview widget?如何在 Treeview 小部件中禁用列调整大小?
【发布时间】:2015-04-06 01:14:39
【问题描述】:

我在 Python 中设置了一个非常基本的 Treeview

self.tv = ttk.Treeview(top_frame, columns=("#","ID","Name"), selectmode = "browse" )

self.tv.heading('#1', text='#', anchor=W)
self.tv.heading('#2', text='ID', anchor=W)
self.tv.heading('#3', text='Name', anchor=W)

self.tv.column('#1',minwidth=70, width = 70, stretch=NO)
self.tv.column('#2', minwidth = 240, width = 240, stretch=NO)
self.tv.column('#3', minwidth=260, width = 260, stretch=NO)
self.tv.column('#0', minwidth=0, width=0, stretch=NO)

我遇到的问题是可以调整列的大小以使树视图比其容器更宽或更窄。两者都破坏了整个事物的美感。

根据我的阅读,stretch = NO 应该禁用此功能,但事实并非如此。我正在使用 Python 2.7.9 在 Mac 上测试 GUI。我知道某些小部件在 Mac 上不能 100% 正常工作,所以我做错了什么,还是我只能期待?

【问题讨论】:

  • (面向未来的读者)当<Button-1> 事件位于树视图分隔符上方时,可以捕获并阻止它,因为我将in this answer 概述为一个明显重复的问题。

标签: python tkinter treeview


【解决方案1】:

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

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


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

我希望我能帮助所有需要解决它的人。

【讨论】:

    【解决方案2】:

    column() 的拉伸参数在 Python ttk 中采用 True/False。 “stretch=False”在 OSX 上对我有用。 见https://docs.python.org/2/library/ttk.html#ttk.Treeview.column

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 2018-09-17
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多