Listbox(列表框)组件用于显示一个选择列表。Listbox 只能包含文本项目,并且所有的项目都需要使用相同的字体和颜色。根据组件的配置,用户可以从列表中选择一个或多个选项。
import tkinter as tk
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('400x200+%d+%d'%((width-400)/2,(height-200)/2))
#创建一个空列表
theLB = tk.Listbox(window)
theLB.pack()
# 往列表里添加数据
for item in ["国家", "省", "市", "区"]:
theLB.insert("end", item)
window.mainloop()
- 获取选择的值
import tkinter as tk
def A():
param = theLB.get(theLB.curselection())
print("选中的值",param)
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('400x200+%d+%d'%((width-400)/2,(height-200)/2))
#创建一个空列表
theLB = tk.Listbox(window)
theLB.pack()
# 往列表里添加数据
for item in ["国家", "省", "市", "区"]:
theLB.insert("end", item)
tk.Button(window,text="获取选中的值",command=A).pack()
window.mainloop()
- 删除选中的值
import tkinter as tk
def A():
param = theLB.get(theLB.curselection())
print("选中的值",param)
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('400x300+%d+%d'%((width-400)/2,(height-300)/2))
#创建一个空列表
theLB = tk.Listbox(window)
theLB.pack()
# 往列表里添加数据
for item in ["国家", "省", "市", "区"]:
theLB.insert("end", item)
tk.Button(window,text="获取选中的值",command=A).pack()
tk.Button(window,text="删除选中的值",command=lambda x=theLB: x.delete("active")).pack()
window.mainloop()
参数
Listbox(master=None, **options) (class)
master -- 父组件
**options -- 组件选项,下方表格详细列举了各个选项的具体含义和用法:



方法
activate(index)
-- 将给定索引号对应的选项激活(在其文本下方画一条下划线)
bbox(index)
-- 返回给定索引号对应的选项的边框
-- 返回值是一个以像素为单位的 4 元祖表示边框:(xoffset, yoffset, width, height)
-- xoffset 和 yoffset 表示距离左上角的偏移位置
-- 返回的 width 是文本的实际宽度(像素为单位)
-- 如果指向的选项是不可见的,那么返回值是 None
curselection()
-- 返回一个元组,包含被选中的选项的序号(从 0 开始)
-- 如果没有选中任何选项,返回一个空元组
delete(first, last=None)
-- 删除参数 first 到 last 范围内(包含 first 和 last)的所有选项
-- 如果忽略 last 参数,表示删除 first 参数指定的选项
get(first, last=None)
-- 返回一个元组,包含参数 first 到 last 范围内(包含 first 和 last)的所有选项的文本
-- 如果忽略 last 参数,表示返回 first 参数指定的选项的文本
index(index)
-- 返回与 index 参数相应的选项的序号(例如 lb.index("end"))
insert(index, *elements)
-- 添加一个或多个项目到 Listbox 中
-- 使用 lb.insert("end") 添加新选项到末尾
itemcget(index, option)
-- 获得 index 参数指定的项目对应的选项(由 option 参数指定)
itemconfig(index, **options)
-- 设置 index 参数指定的项目对应的选项(由可变参数 **option 指定)
nearest(y)
-- 返回与给定参数 y 在垂直坐标上最接近的项目的序号
scan_dragto(x, y)
-- 见下方 scan_mark(x, y)
scan_mark(x, y)
-- 使用这种方式来实现 Listbox 内容的滚动
-- 需要将鼠标按钮事件及当前鼠标位置绑定到 scan_mark(x, y) 方法,然后再将 <motion> 事件及当前鼠标位置绑定到 scan_dragto(x, y) 方法,就可以实现 Listbox 在当前位置和 sacn_mack(x, y) 指定的位置 (x, y) 之间滚动
see(index)
-- 调整列表框的位置,使得 index 参数指定的选项是可见的
select_anchor(index)
-- 与 selection_anchor(index) 相同,见下方解释
select_clear(first, last=None)
-- 与 selection_clear(first, last=None) 相同,见下方解释
select_includes(index)
-- 与 selection_includes(index) 相同,见下方解释
select_set(first, last=None)
-- 与 selection_set(first, last=None) 相同,见下方解释
selection_anchor(index)
-- 在 index 参数的位置下一个锚点,此后你就可以通过特殊索引 "anchor" 访问
selection_clear(first, last=None)
-- 取消参数 first 到 last 范围内(包含 first 和 last)选项的选中状态
-- 如果忽略 last 参数,则只取消 first 参数指定选项的选中状态
selection_includes(index)
-- 返回 index 参数指定的选项的选中状态
-- 返回 1 表示选中,返回 0 表示未选中
selection_set(first, last=None)
-- 设置参数 first 到 last 范围内(包含 first 和 last)选项为选中状态
-- 如果忽略 last 参数,则只设置 first 参数指定选项为选中状态
size()
-- 返回 Listbox 组件中选项的数量
xview(*args)
-- 该方法用于在水平方向上滚动 Listbox 组件的内容,一般通过绑定 Scollbar 组件的 command 选项来实现(具体操作参考:Scrollbar)
-- 如果第一个参数是 "moveto",则第二个参数表示滚动到指定的位置:0.0 表示最左端,1.0 表示最右端
--如果第一个参数是 "scroll",则第二个参数表示滚动的数量,第三个参数表示滚动的单位(可以是 "units" 或 "pages"),例如:xview("scroll", 3, "pages")表示向右滚动三行
xview_moveto(fraction)
-- 跟 xview("moveto", fraction) 一样
xview_scroll(number, what)
-- 跟 xview("scroll", number, what) 一样
yview(*args)
-- 该方法用于在垂直方向上滚动 Listbox 组件的内容,一般通过绑定 Scollbar 组件的 command 选项来实现(具体操作参考:Scrollbar)
-- 如果第一个参数是 "moveto",则第二个参数表示滚动到指定的位置:0.0 表示最顶端,1.0 表示最底端
-- 如果第一个参数是 "scroll",则第二个参数表示滚动的数量,第三个参数表示滚动的单位(可以是 "units" 或 "pages"),例如:yview("scroll", 3, "pages") 表示向下滚动三页
yview_moveto(fraction)
-- 跟 yview("moveto", fraction) 一样
yview_scroll(number, what)
-- 跟 yview("scroll", number, what) 一样
网友评论