簡體   English   中英

tkinter ttk treeview 彩色行

[英]tkinter ttk treeview colored rows

我正在嘗試將 colors 設置為 tkinter treeview ZA8CFDE6331_BD59EB2AC96F 中的行,使用tags8911C4B666Z

之前有一個關於着色行的討論,它相當古老,似乎不再適用於 Python3:

ttk treeview:交替行 colors

我添加了一個簡短的示例。 對我來說,所有行都保持白色,與我在插入命令之前還是之后執行 tag_configure 無關。

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
w = tk.Label(root, text="Hello, world!")
w.pack()

lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('gr', background='green')
lb.column("number", anchor="center", width=10)    
lb.insert('',tk.END, values = ["1","testtext1"], tags=('gr',))
lb.insert('',tk.END, values = ["2","testtext2"])

lb.pack()

root.mainloop()

發生了什么變化或我錯過了什么?

編輯:似乎這是一個新的已知錯誤,有解決方法,但我沒有得到這個工作: https://core.tcl-lang.org/tk/tktview?name=509cafae

EDIT2:我現在使用 tk 版本 8.6.10(構建 hfa6e2cd_0,通道 conda-forge)和 python 3.7.3。 任何人都可以使用此版本的 python 和 tk 重現此錯誤嗎?

您不再需要使用 fixed_map 該錯誤已在 tkinter 版本 8.6 中修復。 以下代碼適用於我使用在 Linux 中運行的 tkinter 8.6 和 python 3.8.2。

import tkinter as tk
import tkinter.ttk as ttk

def fixed_map(option):
    return [elm for elm in style.map("Treeview", query_opt=option) if elm[:2] != ("!disabled", "!selected")]

root = tk.Tk()
style = ttk.Style()
style.map("Treeview", foreground=fixed_map("foreground"), background=fixed_map("background"))

w = tk.Label(root, text="Hello, world!")
w.pack()

lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('odd', background='green')
lb.tag_configure('even', background='lightgreen')

lb.column("number", anchor="center", width=10)
lb.insert('', tk.END, values = ["1","testtext1"], tags=('odd',))
lb.insert('', tk.END, values = ["2","testtext2"], tags=('even',))
lb.insert('', tk.END, values = ["3","testtext3"], tags=('odd',))
lb.insert('', tk.END, values = ["4","testtext4"], tags=('even',))

lb.pack()

root.mainloop()

Chuck666 的回答成功了: https://stackoverflow.com/a/60949800/4352930

此代碼有效

import tkinter as tk
import tkinter.ttk as ttk

def fixed_map(option):
    # 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")]



root = tk.Tk()

style = ttk.Style()
style.map("Treeview", 
          foreground=fixed_map("foreground"),
          background=fixed_map("background"))

w = tk.Label(root, text="Hello, world!")
w.pack()

lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('gr', background='green')
lb.column("number", anchor="center", width=10)    
lb.insert('',tk.END, values = ["1","testtext1"], tags=('gr',))
lb.insert('',tk.END, values = ["2","testtext2"])

lb.pack()

root.mainloop()

我希望 Chuck666 在這里復制他的答案,因為我認為如果他出現,他已經獲得了獎金。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM