Using ttk.Treeview, how can I get horizontal scrolling to work as expected?

Using this Python script, which is my first attempt to find out how ttk.Treeview works (an extra copy of autoscrolling is just breaking horizontal and vertical callbacks):

import Tkinter as tk import ttk class Test(): def __init__(self, parent=None): self.win = tk.Toplevel() self.win.grid_columnconfigure(0, weight=1) self.win.grid_rowconfigure(0, weight=1) self.treeFrame = ttk.LabelFrame(self.win, text='Test Scrolling tk.Treeview') self.treeFrame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew') self.treeFrame.grid_columnconfigure(0, weight=1) self.treeFrame.rowconfigure(0, weight=1) def autoscrollv(sbar, first, last): print 'vert first:%s, last:%s' % (first,last) first, last = float(first), float(last) if first <= 0 and last >= 1: sbar.grid_remove() else: sbar.grid() sbar.set(first, last) def autoscrollh(sbar, first, last): print 'horz first:%s, last:%s' % (first,last) first, last = float(first), float(last) if first <= 0 and last >= 1: sbar.grid_remove() else: sbar.grid() sbar.set(first, last) vsb = ttk.Scrollbar(self.treeFrame, orient="vertical") hsb = ttk.Scrollbar(self.treeFrame, orient="horizontal") self.tree = ttk.Treeview(self.treeFrame, height=10, yscrollcommand=lambda f, l: autoscrollv(vsb, f, l), xscrollcommand=lambda f, l: autoscrollh(hsb, f, l)) self.tree.column("#0", minwidth=400, stretch=True) vsb['command'] = self.tree.yview hsb['command'] = self.tree.xview self.tree.grid(row=0, column=0, sticky='nsew') vsb.grid(column=1, row=0, sticky='ns') hsb.grid(column=0, row=1, sticky='ew') self.tree.insert('', 'end', 'xyz', text='abc'*20) for i in range(30): self.tree.insert('xyz', 'end', text='%s' % i + 'xyz'*40) self.win.mainloop() if __name__ == "__main__": test = Test() 

I see the following behavior: the first time the program starts, the output looks like this: initial state .

If I expand the window until the horizontal scrollbar turns off, it will look like this: last just goes to 1.0

If I continue to expand the window, here is the whole text: closed item width

My first question is: how can I get xscrollcommand to recognize the actual width of the text, and not use the value for minwidth?

Returning to the state in the second image, if I open the element, it will go to the following: vscroll on

My second question is almost the same as the first. How can I recognize the new width of an element after it is opened?

If you change minwidth = 400 to width = 400, the horizontal scrollbar will never appear.

Summary: while the vertical scrollbar works as I would expect, and reacting to the actual lines of the displayed text (and not the ttk.Treeview setting for height), the horizontal scrollbar seems to take care of the minwidth value. I thought to use something like below, especially if there is an equivalent for ttk themed widgets. But the minwidth setting does not take effect after filling the tree, although I can change the width value at this point. Is there a way to force change the minwidth?

 minwidth=tkFont.Font().measure(text) self.tree.column('#0', minwidth=tcw) 

Sorry for the extra window; this part of Tkinter doesn't scare me ...

+4
source share

Source: https://habr.com/ru/post/1443769/


All Articles