更改 ttk.Style 会移除 ttk.Treeview 中的突出显示
Changing ttk.Style removes highlighting from ttk.Treeview
我尝试构建一个树视图来将我的数据存储在 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()
出于某种我不知道的原因,创建的主题似乎没有继承自 parent
。因此,您的主题在其他设置中缺少树视图行的动态样式。
为了避免这种情况,我认为修改现有主题更简单:
style = ttk.Style()
style.theme_use('clam')
style.configure(...)
style.map(...)
我尝试构建一个树视图来将我的数据存储在 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()
出于某种我不知道的原因,创建的主题似乎没有继承自 parent
。因此,您的主题在其他设置中缺少树视图行的动态样式。
为了避免这种情况,我认为修改现有主题更简单:
style = ttk.Style()
style.theme_use('clam')
style.configure(...)
style.map(...)