oskros
oskros

Reputation: 3285

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.

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

Upvotes: 0

Views: 171

Answers (1)

j_4321
j_4321

Reputation: 16169

For some reason I don't know, it seems like the created theme does not inherit from 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(...)

Upvotes: 2

Related Questions