tkinter listbox实例

                Listbox
height 列表显示长度
listvariable 列表项目(新版本支持)
selectmode  选择模式,分为单选(browse) 多选(extended)


.curselection() 返回选中的项目
.see(index)  检查该index的项目是否有效
.itemconfigure(index,**kw) 设置目标项的属性,可用属性为
background, bg, foreground, fg, selectbackground, selectforeground
.selection_set(index)  设置选择的项目 


<<ListboxSelect>> 虚拟事件,当选择项变化时触发


from tkinter import *# from tkinter import ttkdef show_msg(*args):    indexs = listbox1.curselection()    index = int(indexs[0])    listbox2.see(index)    listbox2.select_set(index)root = Tk()root.title("listbox练习")#创建列表显示内容names = ("梅长苏","誉王","飞流","夏冬","霓凰郡主","蒙挚","萧景睿","谢玉")players = ("胡歌","黄维德","吴磊","张龄心","刘涛","陈龙","程皓枫","刘奕君"# 刘奕君list1 = StringVar(value=names)list2 = StringVar(value=players)#创建两个Listbox,分别设置为单选、多选类型listbox1 = Listbox(root,height=len(names),listvariable=list1,selectmode="browse")listbox2 = Listbox(root,height=len(players),listvariable=list2,selectmode="extended")listbox1.grid(row=1,column=1,padx=(10,5),pady=10)listbox2.grid(row=1,column=2,padx=(5,10),pady=10)listbox1.select_set(4)# listbox2.select_set(1,5)#设置第二个表格的项目颜色等for i in range(len(players)):    listbox2.itemconfig(i,fg="blue")    if not i%2:        listbox2.itemconfig(i,bg="#f0f0ff")#为第一个Listbox设置绑定事件listbox1.bind("<<ListboxSelect>>",show_msg)root.mainloop()


           

猜你喜欢

转载自blog.csdn.net/qq_44952766/article/details/89456411