简体   繁体   English

更改 ttk.Style 从 ttk.Treeview 中删除突出显示

[英]Changing ttk.Style removes highlighting from ttk.Treeview

I have tried building a treeview to store my data in tkinter, but unfortunately changing the style with ttk.Style completely removes the ability to highlight rows and the feature where columns are highlighted when hovered over.我曾尝试构建一个树视图来将我的数据存储在 tkinter 中,但不幸的是,使用 ttk.Style 更改样式完全消除了突出显示行的能力以及悬停时突出显示列的功能。

Is there any way to fix this?有没有什么办法解决这一问题?

Example:例子:

import tkinter as tk
from tkinter import ttk

inp = [{'Currency': 'EUR', 'Volume': '100', 'Country': 'SE'},
       {'Currency': 'GBP', 'Volume': '200', 'Country': 'SE'},
       {'Currency': 'CAD', 'Volume': '300', 'Country': 'SE'},
       {'Currency': 'EUR', 'Volume': '400', 'Country': 'SE'},
       {'Currency': 'EUR', 'Volume': '100', 'Country': 'DK'},
       {'Currency': 'GBP', 'Volume': '200', 'Country': 'DK'},
       {'Currency': 'CAD', 'Volume': '300', 'Country': 'DK'},
       {'Currency': 'EUR', 'Volume': '400', 'Country': 'DK'},
       ]


class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Volume")

        self.tree = ttk.Treeview(self, show='headings')
        columns = list(inp[0].keys())

        self.tree["columns"] = columns
        self.tree.pack(expand=tk.TRUE, fill=tk.BOTH)

        for i in columns:
            self.tree.column(i, anchor=tk.W)
            self.tree.heading(i, text=i, anchor=tk.W)

        for row in inp:
            self.tree.insert("", "end", values=list(row.values()))


root = Application()
style = ttk.Style()
style.theme_create('my_style', parent='clam')
style.theme_use('my_style')
root.mainloop()

For some reason I don't know, it seems like the created theme does not inherit from parent .出于某种我不知道的原因,似乎创建的主题没有从parent继承。 Therefore your theme is missing among other settings the dynamic styling of the treeview's row.因此,您的主题在其他设置中缺少树视图行的动态样式。

To avoid that, it is simpler in my opinion to just modify an existing theme:为了避免这种情况,我认为只修改现有主题更简单:

style = ttk.Style()
style.theme_use('clam')

style.configure(...)
style.map(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM