如何检测 ttk.Treeview 列的大小调整? [英] How to detect resizing of ttk.Treeview column?

查看:32
本文介绍了如何检测 ttk.Treeview 列的大小调整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ttk.Notebook,每个标签都包含一个 ttk.Treeview.所有树视图都具有相同的列但包含不同的项目,如下面的代码所示.

I have a ttk.Notebook and each tab contains a ttk.Treeview. All treeviews have the same columns but contain different items, like in the code below.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

notebook = ttk.Notebook(root)
notebook.pack()

tree1 = ttk.Treeview(notebook, columns=['a', 'b', 'c'])
tree1.insert('', 'end', text='item1', values=('a1', 'b1', 'c1'))
tree2 = ttk.Treeview(notebook, columns=['a', 'b', 'c'])
tree2.insert('', 'end', text='item2', values=('a2', 'b2', 'c2'))
tree2.insert('', 'end', text='item2', values=('a2', 'b2', 'c2'))

notebook.add(tree1, text='Tab 1')
notebook.add(tree2, text='Tab 2')

root.mainloop()

我希望所有树始终具有相同的列宽.因此,例如,当用户调整 tree1 的a"列的大小时,tree2 的a"列也应调整大小.

I would like all trees to always have the same column widths. So, for instance, when the user resizes the column 'a' of tree1, the column 'a' of tree2 should be resized too.

我知道我可以用tree1.column('a', 'width') 并用 tree2.column('a', width=300) 设置.

I know I can get the size of a column with tree1.column('a', 'width') and set it with tree2.column('a', width=300).

但是如何检测列的大小发生了变化?

But how can I detect that the size of a column has changed?

我已经检查过树视图 事件不是由列调整大小触发的.

I have checked that the treeview <Configure> event is not triggered by a column resizing.

推荐答案

根据 CommonSense 的建议,我对 进行了绑定,以检查列是否已调整大小.如果 tree.identify_region(event.x, event.y)'separator' 那么有一个调整大小.然后我需要识别分隔符两侧的列.tree.identify_column(event.x)'#' 的形式给我左边的列,从中我可以得到右侧的列.最后,我执行调整所有树中列大小的函数.

Following CommonSense suggestions, I have made a binding on <ButtonRelease-1> to check if a column has been resized. If tree.identify_region(event.x, event.y) is 'separator' then there was a resizing. Then I need to identify the columns on both sides of the separator. tree.identify_column(event.x) gives me the column on the left in the form '#<column number>' and from it I can get the id of the column on the right. Finally, I execute the function that resize the columns in all trees.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()


def on_click_release(event):
    tree = event.widget
    if tree.identify_region(event.x, event.y) == 'separator':
        left_column = tree.identify_column(event.x)
        right_column = '#%i' % (int(tree.identify_column(event.x)[1:]) + 1)
        width_l = tree.column(left_column, 'width')
        width_r = tree.column(right_column, 'width')
        for tree2 in trees:
            if tree2 != tree:
                tree2.column(left_column, width=width_l)
                tree2.column(right_column, width=width_r)


notebook = ttk.Notebook(root)
notebook.pack()

trees = [ttk.Treeview(notebook, columns=['a', 'b', 'c']) for i in range(4)]

for i, tree in enumerate(trees):
    tree.bind('<ButtonRelease-1>', on_click_release)
    notebook.add(tree, text='Tab %i' % i)

root.mainloop()

EDIT:我意识到如果我们将列分隔符移动得太快,上述方法将不起作用(tree.identify_region(event.x, event.y)不返回分隔符").于是我找到了一个不同的方法:当用户更改tab时,则将当前tab的每一列的宽度设置为之前可见的tab对应列的宽度.

EDIT: I realized that the above method does not work if we move the column separator too quickly (tree.identify_region(event.x, event.y) does not return 'separator'). So I have found a different method: When the user changes tab, then the width of each column of the current tab is set to the width of the corresponding column of the previously visible tab.

import tkinter as tk
from tkinter import ttk


def tab_changed(event):
    global current_tab
    tab = notebook.index('current')  # get newly visible tab number
    tree1 = trees[current_tab]  # get previously visible tree
    tree2 = trees[tab]   # get newly visible tree
    cols = ('#0', ) + tree1.cget('columns')  # tuple of all columns
    for column in cols:
        tree2.column(column, width=tree1.column(column, 'width'))
    current_tab = tab


root = tk.Tk()
notebook = ttk.Notebook(root)
notebook.pack()

trees = [ttk.Treeview(notebook, columns=['a', 'b', 'c']) for i in range(4)]
current_tab = 0  # store currently visible tab number

for i, tree in enumerate(trees):
    notebook.bind('<<NotebookTabChanged>>', tab_changed)
    notebook.add(tree, text='Tab %i' % i)

root.mainloop()

这篇关于如何检测 ttk.Treeview 列的大小调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆