Отступы tkinter Treeview - PullRequest
       42

Отступы tkinter Treeview

0 голосов
/ 10 июля 2020

Я создаю древовидную структуру (см. Ссылку на изображение ниже) в приложении и хочу настроить отступ дочерних элементов в дереве. Я использовал style.configure для установки уровня отступа (см. Пример кода ниже). Но, похоже, это контролирует только отступ первого дочернего элемента на родительском уровне. Я хотел бы установить одинаковый отступ для всех дочерних уровней.

Вот фрагмент кода, который я использую. Он показывает конфигурацию стиля и определение первой вкладки, показанной на изображении:

style = ttk.Style()
style.configure("Mystyle.Treeview",  indent=15,  font=(self.tree_font, self.tree_font_size, 'normal') )
style.layout("Mystyle.Treeview", [('Mystyle.Treeview.treearea', {'sticky': 'nswe'})])  # Remove the borders


self.NBframe = Frame(self.navFrame, width=tree_display_width)
self.NBframe.pack(expand = TRUE, fill=Y)
self.NBframe_h_scrollbar = Scrollbar(self.NBframe, orient='horizontal')
self.NBframe_h_scrollbar.pack(side=BOTTOM, fill=X)
self.NBframe_v_scrollbar = Scrollbar(self.NBframe, orient='vertical')
self.NBframe_v_scrollbar.pack(side=RIGHT, fill=Y)
self.navControl = ttk.Notebook(self.NBframe, width=tree_display_width)
self.navControl.pack(expand = TRUE, fill=Y)
self.navControl.bind('<Button-1>', self._on_navControl_click)

self.tabVendors = Frame(self.navControl, background="white", borderwidth=1, relief=RAISED, width=tree_display_width)
self.tabVendors.pack()
self.Vendors_tab_name = "Vendors"
self.navControl.add(self.tabVendors, text=self.Vendors_tab_name)
self.Vendors_tab_index = self.navControl.index(self.tabVendors)
self.tabVendors_tree = ttk.Treeview(self.tabVendors,  style="Mystyle.Treeview",show="tree",selectmode = "extended")
self.tabVendors_tree.column("#0", minwidth=tree_scroll_width, stretch=YES)
self.tabVendors_tree.pack(side=TOP, anchor=NW, expand=YES,fill=BOTH)

Вот снимок экрана получившегося дерева:

screenshot of tree structure

In the example, I can control the indentation of "ACTION SOFTWARE" and "AMERICAN SOFT." by using the style.configure. But I can't seem to change the indentation of the children below those entries.

Update: What I have noticed is that changing the indent= does change the lower level children indentations. For example, if I set indent=75, the lower level children are indented. But there seems to be a minimum indention number for lower level children. If I set indent=0, then the second level children line up under the parent, but the lower level children are still indented (line up under the first character of parent above them).

I'm wondering if this might be related to the open/close indicator not being present for the lower level children?

Here is a code snippet that can be used to test this scenario.

from tkinter import ttk
from tkinter import *

win = Tk()

windowFrame = Frame(win, relief=RIDGE)
windowFrame.pack(side=TOP, fill=BOTH, expand=YES)

tabbed_Frame = Frame(windowFrame)
tabbed_Frame.pack(side=TOP,fill=BOTH, expand=YES )
navFrame = Frame(tabbed_Frame, padx=4, pady=4, borderwidth=1, relief=SUNKEN)
navFrame.pack(side=LEFT, fill=BOTH)
dbLabel = Label(navFrame)
dbLabel.pack(side=TOP, pady=2,anchor=W)
dbLabel["text"] = "Active database:"
dbSelect = ttk.Combobox(navFrame, width=37,
                    values=["TEST.QDB"])
dbSelect.pack(side=TOP, pady=2,anchor=W)

style = ttk.Style()
style.configure("Mystyle.Treeview",  indent=15 , bd=0)
style.layout("Mystyle.Treeview", [('Mystyle.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders

tree_width=500
NBframe = Frame(navFrame)
NBframe.pack(expand = TRUE, fill=Y)

NBframe_h_scrollbar = Scrollbar(NBframe, orient='horizontal')
NBframe_h_scrollbar.pack(side=BOTTOM, fill=X)
NBframe_v_scrollbar = Scrollbar(NBframe, orient='vertical')
NBframe_v_scrollbar.pack(side=RIGHT, fill=Y)

navControl = ttk.Notebook(NBframe)
navControl.pack(expand = TRUE, fill=Y)
tabVendors = Frame(navControl, background="white", borderwidth=1, relief=None, width=250)
tabVendors.pack()
Vendors_tab_name = "Vendors"
navControl.add(tabVendors, text=Vendors_tab_name)
Vendors_tab_index = navControl.index(tabVendors)
tabVendors_tree = ttk.Treeview(tabVendors,show="tree",style="Mystyle.Treeview", selectmode = "extended")
tabVendors_tree.column("#0", minwidth=tree_width, stretch=YES)
tabVendors_tree.pack(side=TOP, anchor=NW, expand=YES,fill=Y)

NBframe_v_scrollbar.config(command=tabVendors_tree.yview)
NBframe_h_scrollbar.config(command=tabVendors_tree.xview)

tabVendors_tree.config(yscrollcommand = NBframe_v_scrollbar.set)
tabVendors_tree.config(xscrollcommand = NBframe_h_scrollbar.set)

tabProducts = Frame(navControl,background="white",borderwidth=1,relief=RAISED, width=230)
tabProducts.pack()
Products_tab_name = "Products"
navControl.add(tabProducts, text = Products_tab_name)
Products_tab_index = navControl.index(tabProducts)
tabProducts_tree = ttk.Treeview(tabProducts, style="Mystyle.Treeview", show="tree",selectmode = "extended",  yscrollcommand = NBframe_v_scrollbar.set)
tabProducts_tree.column("#0", minwidth=tree_width, stretch=YES)
tabProducts_tree.pack(side=TOP, anchor=NW, expand=YES, fill=Y)

tabVendors_tree.insert("", "end", "Level1", text="Level-1")
index=0
for i in range(0, 5):

    tree_item=tabVendors_tree.insert("Level1", "end", index, text = "Level-2-"+str(i))
    index+=1
    for j in range (0,20):

        tabVendors_tree.insert(tree_item, "end", index, text="Level-2-"+ str(i) + '-' + str(j) + 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxy')
        index+=1

win.mainloop()
quit()

Is there another option that I am overlooking?

UPDATE:

Here is an image of the test code with style.configure("Mystyle.Treeview", indent=15 , bd=0). This causes the open/close box for Level-2-0 to align under the "L" in its parent, Level-1. My goal is to get the "Level-2-0-0xxxx...." entry (highlighted) to align under the "L in its 'Level-2-0' parent entry above it. However, it is still indented. It seems that there is a minimum indention for the lowest level children.

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...