使用主题 ttk 树视图时 tag_configure 不起作用 [英] tag_configure is not working while using theme ttk treeview
问题描述
我创建了一个 Treeview 并使用以下代码更改了所选颜色:
i have created a Treeview and changed the selected color using this code:
style = self.style = ttk.Style(win)
style.theme_create("my", "vista",
settings={
'Treeview': {
'map': {
'background': [('selected', '#ffdddd'), ("!selected", "white")],
'foreground': [('selected', 'black')],
"font": [("", ("", 13))]
} # end 'map'
}, # end 'Treeview'
'TNotebook.Tab': {
'map': {
'font': [("", ("", 14))]
} # end 'map'
}, # end 'TNotebook.Tab'
'TNotebook': {
'map': {
'background': [("", "#eee")]
} # end 'map'
} # end 'TNotebook
} # end settings
)
style.theme_use("my")
代码工作正常,但是当我尝试使用 tag_configure 方法更改特定项目(行)的背景颜色时,没有任何变化,我发现这是一个 tkinter 错误,解决方案是以下代码:(它在不使用以上主题)
The code works fine, but when i try to change background color of specific item's (rows) using tag_configure method nothing changes, i found that this is a tkinter bug, the solution is this code: (it is working without using the above theme)
def fixed_map(option):
# Fix for setting text colour for Tkinter 8.6.9
# From: https://core.tcl.tk/tk/info/509cafafae
#
# Returns the style map for 'option' with any styles starting with
# ('!disabled', '!selected', ...) filtered out.
# style.map() returns an empty list for missing options, so this
# should be future-safe.
return [elm for elm in style.map('Treeview', query_opt=option) if
elm[:2] != ('!disabled', '!selected')]
style.map('Treeview', foreground=fixed_map('foreground'), background=fixed_map('background'))
所以问题是当我想指定选定的背景时,我无法更改项目的背景颜色并且 tag_configure 不会影响任何东西.我是否遗漏了什么或有其他方法可以做到这一点?
so the problem is while i want to specify the selected background, i can't change background color of items and tag_configure is not affecting anything. am i missing something or is there any other way to do this?
推荐答案
在 Windows 10、Python 3.9.0 上,我遇到了同样的问题.使用 tag_configure 更改字体工作正常,但不是背景或前景.我是 tkinter 的新手,在这个问题上花了很多时间,并在我找到了解决方法后找到了上述解决方案 - 使用实际使用的主题的副本,一切都可以正常工作.
On windows 10, Python 3.9.0, I've the same problem. Altering font with tag_configure works fine, but not background nor foreground. I'm new to tkinter, spend many hours on this problem and found the above solution after I had a workaround - with a copy of the actual used theme everything seams to work.
我的解决方法:
style = ttk.Style(self)
aktualTheme = style.theme_use()
style.theme_create("dummy", parent=aktualTheme)
style.theme_use("dummy")
隔离问题的程序.使用 zebra = 0(第 10 行),我没有得到预期的结果
The program to isolate the problem. With zebra = 0 (line 10), I don't get the expected result
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title('Treeview Zebra')
self.geometry('620x250')
zebra = 1
if zebra:
style = ttk.Style(self)
aktualTheme = style.theme_use()
style.theme_create("dummy", parent=aktualTheme)
style.theme_use("dummy")
self.tree = ttk.Treeview(self, columns=('F1', 'F2'), show='headings')
self.tree.heading('F1', text='Field 1')
self.tree.heading('F2', text='Field 2')
self.tree.pack()
# tags to tree
self.tree.tag_configure('odd', background='#E6B3FF')
self.tree.tag_configure('even', background='#DD99FF', foreground='white')
self.tree.tag_configure('A10', background='#E6B3FF', font='Arial 10')
self.tree.tag_configure('S', background='#DFDFDF', font=('Calibri', 9, 'bold'))
# adding generated sample data to the treeview
for n in range(1, 7):
if n % 2 :
self.tree.insert('', tk.END, values=(f'first {n}', f'last {n}', f'email{n}@example.com'), tags = ('S'))
else:
self.tree.insert('', tk.END, values=(f'first {n}', f'last {n}', f'email{n}@example.com'), tags = ('even', 'A10'))
if __name__ == "__main__":
app = App()
app.mainloop()
这篇关于使用主题 ttk 树视图时 tag_configure 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!