如何制作ttk。Ttreeview 行覆盖了整个可用高度?

How to make ttk.Ttreeview rows cover the whole available height?

提问人:DWK 提问时间:11/1/2023 最后编辑:DWK 更新时间:11/1/2023 访问量:89

问:

一天中的好时光!

我的 Treeview 小部件会遵循“新闻”方向,因此每当我调整应用程序大小时,它都会随之缩小/扩展。问题是,当 Treeview 的扩展不足以容纳另一行时,它会留下一个空白区域。(空白处)

有没有办法让行始终覆盖整个 Treeview 高度,就像 tk 一样。列表框有吗? (想要的结果)

到目前为止,我已经尝试调整行高参数,但显然没有帮助,因为仍然有足够空间容纳另一行的时刻。

这是我到目前为止的代码(整个应用程序代码相当长,所以我只留下与 Treeview 相关的代码片段):

初始化 Treeview 小部件:

self.colorTreeView = ttk.Treeview(self.colorListSubFrame, columns=['savedColors'], show='tree', selectmode=tk.BROWSE, style="DARK_THEME.Treeview", height=11)
self.colorTreeView.heading("savedColors", text="Colors", anchor="nw")
self.colorTreeView.column("savedColors", width=10, anchor='center', stretch=True)
self.colorTreeView.grid(column=0, row=0, sticky="news", padx=5, pady=12)

与 Treeview 交互的方法:

def SaveColor(self, *args):
    newColor = ColorConvertor.Color(self.currentPixelColor)
    self.colorList.insert(0, newColor)
    self.colorTreeView.tag_configure(newColor, background= '#'+ newColor.stringColorValueHEX)
    if newColor.colorName is not None:
        t = self.colorTreeView.insert('', 0, text= newColor.colorName, tags=(newColor,))
    else:
        t = self.colorTreeView.insert('', 0,text= eval("newColor.stringColorValue" + f"{self.currentColorSpace}"), tags=(newColor,))


def RefreshList(self):
    self.colorTreeView.delete(*self.colorTreeView.get_children())
    for i in self.colorList:
        self.colorTreeView.insert('', tk.END,text= eval("i.stringColorValue" + f"{self.currentColorSpace}"), tags=(i,))


def SwitchColorSpace(self, *args):
    _newCS = self.colorSpaceCB.get()
    self.colorSpaceCB.selection_clear()
    self.currentColorSpace = _newCS
    self.RefreshList()
    self.colorTreeView.update()

我的风格设置:

DARK_THEME_SETTINGS ={
        'DARK_THEME_MAIN-FRAME.TFrame': {
            'configure':{
                'background': '#1F1F1F',
            }
        },
        'TEST_THEME_MAIN-FRAME.TFrame': {
            'configure':{
                'background': 'orange',
            }
        },
        'DARK_THEME_IMAGEBOX.TLabel': {
            'configure':{
                'relief': 'solid',
                'borderwidth': 2,
                'bordercolor': "white",
            },
        },
        'DARK_THEME_INFO-TEXT.TLabel':{
            'configure':{
                'foreground': 'white',
            }
        },
        'DARK_THEME.Treeview': {
            'configure': {
                'background': '#1F1F1F"',
                'foreground': 'white',
                'fieldbackground': '#1F1F1F',
                'font': ('Segoe UI', 9),
                'padding': 0,
                'relief': 'solid',
                'borderwidth': 1,
                'bordercolor': 'white',
                'rowheight': 20,
            },
            'map': {
                'font': [('selected', ('Segoe UI', 9, 'bold'))],
                'foreground': [('selected', 'white')],
            }
        },
        'DARK_THEME.TCombobox': {
            'configure': {
                'foreground': 'white',
                'background': "#1F1F1F",
                'fieldbackground': "#1F1F1F",
                'selectbackground' : '#0066D1',
                'selectforeground': "white",
                'bordercolor': 'white',
                'borderwidth': 0.5,
                'arrowcolor': 'white',
                'relief': "flat",
            },
            'map': {
                'foreground': [('active', "#1F1F1F"), ('disabled', "#1F1F1F")],
                'background': [('active', "#1F1F1F"), ('disabled', "#1F1F1F")],
                'arrowcolor': [('active', 'white'), ('disabled', 'white')],
            },
        },
        'DARK_THEME.TEntry':{
            'configure': {
                'foreground': 'white',
                'background': "#1F1F1F",
                'fieldbackground': "#1F1F1F",
                'selectbackground' : '#0066D1',
                'relief': "solid",
                'bordercolor': 'white',
                'borderwidth': 1,
            }
        }
    }
蟒蛇 tkinter 树视图 ttk

评论

0赞 Andrej Kesely 11/1/2023
也许您可以编辑您的问题并添加您拥有的代码。
0赞 DWK 11/1/2023
@Andrej Kesely,当然!
0赞 acw1668 11/1/2023
由于树视图中只有一列,那么为什么不改用呢?Listbox
0赞 DWK 11/1/2023
@acw1668因为 ttk 没有 Listbox,只有 Treeview。这意味着我必须采取一些解决方法,以保持其风格与应用程序的其余部分保持一致。
0赞 toyota Supra 11/2/2023
如果你删除关键字怎么办?selectmode=tk.BROWSE

答: 暂无答案