如何在 Tkinter 中更改树视图的背景颜色?


树视图小部件旨在以分层结构显示数据。它可用于显示目录、子目录或文件,形式为列表。列表框中存在的项称为列表框项。

树视图小部件包含许多属性,我们可以通过它们更改或修改其默认属性。我们可以通过在构造函数中定义“背景”属性来更改树视图小部件的背景。

示例

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Create a Listbox widget
lb = Listbox(win, width=100, height=10, background="purple4", foreground="white", font=('Times 13'),selectbackground="black")
lb.pack()


# Select the list item and delete the item first
# Once the list item is deleted, we can insert a new item in the listbox
def edit():
   for item in lb.curselection():
      lb.delete(item)
      lb.insert("end", "foo")


# Add items in the Listbox
lb.insert("end", "item1", "item2", "item3", "item4", "item5")

# Add a Button To Edit and Delete the Listbox Item
ttk.Button(win, text="Edit", command=edit).pack()

win.mainloop()

输出

如果我们运行上述代码,它将显示一个带有树视图小部件的窗口,该小部件具有独特的背景颜色和一些元素。

更新于: 05-Aug-2021

3 千 + 次查看

开启您的 职业

完成课程取得认证

开始学习
广告
© . All rights reserved.