繁体   English   中英

如何在 Tkinter Treeview 中获取列宽

[英]How to GET the column width in a Tkinter Treeview

有没有办法获取 tk.treeview 的所有树视图列的宽度?

我知道如何设置宽度,但我想在用户退出窗口后将其自定义更改保存在列表中,以便在重新启动后重新调用。

到目前为止,我一切都在运行,我只是在搜索(但找不到)关于如何获取列宽的答案。

这是一个最小的代码示例:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Minimal Treeview")


treeview = ttk.Treeview(
    root,
    columns=["col1", "col2", "col3"],
    height="10")
treeview.column(0, width=300, anchor="w")
treeview.column(1, width=500, anchor="w")
treeview.column(2, width=60, anchor="w")
treeview.pack()


def on_exit(event):
    # Desired code:
    # column_width = treeview.columnswidth
    # column_width holds something like [233, 500, 48]
    print("Exit Program")


# Before exiting, user changes the column width manually with the mouse
root.bind("<Destroy>", on_exit)
root.mainloop()

每一个帮助表示赞赏! :-) 非常感谢您并享受您的编码。

只需使用列名和选项'width'调用 c column()

print(treeview.column('col1', 'width'))

# => 300 (returns the column width as an integer)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM