如何在 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 千 + 次檢視

開啟您的 職業

完成課程取得認證

開始學習
廣告