How To Detect Resizing Of Ttk.Treeview Column?
Solution 1:
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: 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()
Post a Comment for "How To Detect Resizing Of Ttk.Treeview Column?"