2011-03-18 2 views

답변

18

Listbox 위젯은 spefic 항목의 색상은 변경할 수 없습니다 관한 effbot.org 문서에 따르면 : 목록 상자는 텍스트 항목을 포함 할 수 있습니다

을, 모든 아이템이 같은 글꼴과 색상

이 있어야합니다

실제로 Listbox 개체의 itemconfig 메서드를 사용하여 특정 항목의 글꼴과 배경색을 모두 변경할 수 있습니다. 다음 예를 참조하십시오.

import tkinter as tk 


def demo(master): 
    listbox = tk.Listbox(master) 
    listbox.pack(expand=1, fill="both") 

    # inserting some items 
    listbox.insert("end", "A list item") 

    for item in ["one", "two", "three", "four"]: 
     listbox.insert("end", item) 

    # this changes the background colour of the 2nd item 
    listbox.itemconfig(1, {'bg':'red'}) 

    # this changes the font color of the 4th item 
    listbox.itemconfig(3, {'fg': 'blue'}) 

    # another way to pass the colour 
    listbox.itemconfig(2, bg='green') 
    listbox.itemconfig(0, foreground="purple") 


if __name__ == "__main__": 
    root = tk.Tk() 
    demo(root) 
    root.mainloop() 
+0

아 ... 감사합니다. 저는 파이썬에 익숙하지 않고 .configure (bg = "Green")를 사용하려고했습니다. –

관련 문제