我編寫了以下代碼來綁定事件并對單個列表框項目執(zhí)行操作。import tkinter as tkroot = tk.Tk()custom_list = tk.Listbox(root)custom_list.grid(row=0, column=0, sticky="news")def onselect_listitem(event): w = event.widget index = int(w.curselection()[0]) value = w.get(index) print(index, value, " color : ",custom_list.itemcget(index,'background')) custom_list.itemconfig(index, fg='gray', selectforeground="gray")custom_list.bind('<Double-Button-1>', onselect_listitem)for k in range(20): custom_list.insert(k, " --------- " + str(k))root.mainloop()在itemconfig正常工作的同時,我無法使用itemcget獲取背景屬性。其他一切都在工作。有人可以告訴我有什么問題嗎?我正在嘗試通過列表框中項目的索引獲取當前項目的背景色。與custom_list.itemcget的部分不打印任何內(nèi)容。
1 回答

臨摹微笑
TA貢獻1982條經(jīng)驗 獲得超2個贊
.itemcget(index, option)
檢索列表框中特定行的選項值之一。有關(guān)選項值,請參見
itemconfig
下文。如果尚未為給定行設置給定選項,則返回值將為空字符串。
因此,由于您尚未設置該background
選項,因此itemcget
將返回一個空字符串。您可以通過將打印更改為來查看此工作custom_list.itemcget(index,'fg')
。第一次雙擊時會收到一個空字符串,因為您尚未設置它,而第二次則顯示gray
。
添加回答
舉報
0/150
提交
取消