tkinter-listbox详解

本文介绍Listbox控件。

Listbox控件的显示主体就是一个选项列表(items list),每个item一行。当创建一个Listbox控件对象时,该对象没有选项(item)。item可以支持两种方法创建。item可以被删除。一个或多个item可以被选中,然后做相应的事情。还是那个套路,结合实例介绍一下常用的属性和方法。

Listbox

class Listbox(Widget, XView, YView)看到这个类的继承关系,就知道可以使用scrollbar。

Listbox(master=None, cnf={}, **kw) master还是指父窗口对象

bg

设置背景颜色

bd

设置边框宽度

fg

设置前景色(文字颜色)

cursor

设置光标显示样式

listvariable

如果你阅读过本系列文章的checkbutton或radiobutton,你将很容易的理解这个属性的作用。listvariable的值是一个StringVar变量,该变量的值是一个列表或元组,用来作为listbox的item。

selectmode

selectmode决定了item怎样被选择的问题?是选一个呢还是选多个?要怎么实现这个"选"?

browse - 默认值。只能选一个选项,即使你按住鼠标拖动,也是选择最后鼠标停留的那个选项

extended - 通过按住鼠标拖动,选择多个选项

singe - 也是只能选择一个选项且只支持鼠标单击,鼠标按住并拖动无效,这也是与browse有差异的地方,但是我并没有感觉这个属性有啥用

multiple - 选择多个选项且支持鼠标单击,鼠标按住并拖动无效

我们来看个简单的例子

from tkinter import Tk,Listbox,StringVar

main_win = Tk()
main_win.title('渔道的listbox控件')
width = 300 
height = 300 
main_win.geometry(f'{width}x{height}')

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectmode='multiple').pack() # 自行替换selectmode的值来感受不同值的差异

main_win.mainloop()

上面这个例子介绍了怎样给listbox添加items,但是如果想动态的增加item呢?那就需要用到insert()函数。

index(index)

获取index指定的item的索引号

insert(index, *elements)

listbox的常见index有如下几种:

  • number - item的索引,从0开始计数
  • active - 表示listbox的当前光标选中的item。
  • anchor - Indicates the anchor point for the selection
  • end - 表示listbox的最后一个item
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross')
l.pack()

for item in range(100):
    l.insert("end", item) 
    
print(l.index("active")) # 输出 0
print(l.index("anchor")) # 输出 100

对listbox实现不同方式的item插入

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items)
l.pack()

items2 = ['watermelon', 'banana']

# 顺序头插
# for i, item in enumerate(items2):
#     print(l.index("active"))
#     print(l.index("anchor"))
#     l.insert(i, item)

# 逆序头插
# for item in items2:
#     print(l.index("active"))
#     print(l.index("anchor"))
#     l.insert(0, item)

# 尾插
for item in items2:
    print(l.index("active"))
    print(l.index("anchor"))
    l.insert("end", item)

三种插入方式,自行执行,观察其差异。

item不仅支持插入也支持删除,使用delete()函数来实现

delete(first, last=None)

删除firstlast的item

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectmode='extended')
l.pack()

l.delete(1,2) # 删除第1个和第2个item

在这里插入图片描述

get(first, last=None)

获取first到last的item 列表

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectmode='extended')
l.pack()

print(l.get(1,2)) # 输出 ('orange', 'pear')

curselection()

获取当前被选中的item(s)的索引号

selection_set(first, last=None)

设置firstlast的item(s)被选中。也可使用select_set()。

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectmode='extended')
l.pack()

print(l.curselection()) # 输出为 空元组 ()
l.select_set(1, 3)
print(l.curselection()) # 输出为 (1, 2, 3)

在这里插入图片描述

size()

获取listbox中item的个数

itemconfigure(index, cnf=None, **kw)

获取index指定的item的属性(background, bg, foreground, fg, selectbackground, selectforeground)

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items)
l.pack()

print(l.size()) # 输出 4
print(l.itemconfig(0))
# {'background': ('background', 'background', 'Background', '', ''), 'bg': ('bg', '-background'), 'fg': ('fg', '-foreground'), 'foreground': ('foreground', 'foreground', 'Foreground', '', ''), 'selectbackground': ('selectbackground', 'selectBackground', 'Foreground', '', ''), 'selectforeground': ('selectforeground', 'selectForeground', 'Background', '', '')}

selectforeground

选中的item的文字的颜色

selectbackground

选中的item的背景的颜色

selectborderwidth

选中的item的边框宽度

items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectbackground='red', selectforeground='blue', selectborderwidth=5, activestyle='dotbox')
l.pack()

在这里插入图片描述

activestyle

表示当某个item被选中后,该item的显示样式。

  • dotbox - 该样式是 选中的item(s)的外围矩形边界不是常规的实线,而是点线。
  • none - 没有样式显示
  • underline - 选中的item(s)的文字有下划线,默认样式
items = ('apple', 'orange', 'pear', 'grape')
strvar_items = StringVar(value=items)
l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectbackground='red', selectforeground='blue', selectborderwidth=20, activestyle='dotbox')
# l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectbackground='red', selectforeground='blue', selectborderwidth=20, activestyle='none')
# l = Listbox(main_win, bg='yellow', bd=10, cursor='cross', listvariable=strvar_items, selectbackground='red', selectforeground='blue', selectborderwidth=20, activestyle='underline')
l.pack()

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

因为类间关系,Listbox可以和scrollbar结合使用。scrollbar和其他控件的组合原理之前已在tkinter-Scrollbar详解一文中做了详细的阐述(Text和Scrollbar)。有了前面的基础,就很容易使用scrollbar和相关控件的关联,这里给出一个示例(Listbox和Scrollbar)

xscrollcommand

指定与水平滚动条的回调函数

yscrollcommand

指定与垂直滚动条的回调函数

l = Listbox(main_win, bg='yellow', bd=10, cursor='cross')
l.pack(side=LEFT, fill=BOTH)
scrollbar_v = Scrollbar(main_win)
scrollbar_v.pack(side=LEFT, fill=Y)

for item in range(100):
    l.insert("end", item)

scrollbar_v.config(command=l.yview)
l.config(yscrollcommand=scrollbar_v.set)

在这里插入图片描述

测试发现,Listbox没有command属性,那么需要使用bind函数来关联事件。本文的重点不是bind机制,会另写一篇文章详细介绍。

bind(sequence=None, func=None, add=None)

def select_cb(*args):
    idxs = l.curselection()
    print(f'idxs:{idxs}')


l = Listbox(main_win, bg='yellow', bd=10, cursor='cross')
l.pack(side=LEFT, fill=BOTH)
scrollbar_v = Scrollbar(main_win)
scrollbar_v.pack(side=LEFT, fill=Y)

for item in range(100):
    l.insert("end", item)

scrollbar_v.config(command=l.yview)
l.config(yscrollcommand=scrollbar_v.set)

l.bind('<<ListboxSelect>>', select_cb) # 将item的选择事件和select_cb()绑定

通过bind函数绑定 虚拟事件ListboxSelect,每选择一个item,就会触发select_cb调用。

  • 13
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sif_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值