【问题标题】:Changing ttk.Style removes highlighting from ttk.Treeview更改 ttk.Style 会从 ttk.Treeview 中删除突出显示
【发布时间】:2020-11-06 15:09:49
【问题描述】:

我尝试构建一个树视图来将我的数据存储在 tkinter 中,但不幸的是,使用 ttk.Style 更改样式完全消除了突出显示行的能力以及悬停时突出显示列的功能。

有没有办法解决这个问题?

例子:

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()

【问题讨论】:

    标签: python tkinter treeview ttk


    【解决方案1】:

    由于某种我不知道的原因,创建的主题似乎不是从parent 继承的。因此,您的主题在其他设置中缺少树视图行的动态样式。

    为了避免这种情况,我认为只修改现有主题会更简单:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-07
      • 2021-07-16
      • 1970-01-01
      • 2011-08-31
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      相关资源
      最近更新 更多