Tkinter (09) 列表框部件 Listbox

列表框部件的创建及其选项#

import tkinter as tk
parent = tk.Tk()
w = tk.Listbox(parent, option, ...)
选项 说明
activestyle active 行的样式,‘underline’ (内定,底线), ‘dotbox’ (点线框), ‘none’ (无)
bg or background 背景颜色
bd or borderwidth 外框寛度,内定为 2
cursor 在部件上方时,鼠标的图样
disabledforeground disabled 时,前景颜色
exportselection 文本可否复制到剪贴板 0/1
font 文本字体
fg or foreground 前景颜色
height 行数,内定为 10
highlightbackground 非聚焦时的聚焦颜色
highlightcolor 聚焦时的聚焦颜色
highlightthickness 聚焦厚度,内定 1, 0 则无
listvariable StringVar 一个字符串变量,内容为 tuple 字符串,如 “(‘v0’, ‘v1’, …)”
relief 外框花样,内定为 SUNKEN
selectbackground 被选择文本的背景颜色
selectborderwidth 被选择文本的外框寛度
selectforeground 被选择文本的前景颜色
selectmode 可选择的行数及拖弋效果,BROWSE (内定,单行可拖弋), SINGLE (单行,拖弋无效), MULTIPLE (点击多选), EXTENDED (拖弋多选)
state NORMAL, DISABLED
takefocus TAB 键在部件是否会循环焦点 0/1
width 部件字寛,内定为 20 个标准字寛
xscrollcommand 水平滚动条 scrollbar.set () 方法
yscrollcommand 垂直滚动条 scrollbar.set () 方法

选项的索引方式#

  • 整数值,从 0 开始,为索引值
  • END 指最后一项
  • ACTIVE 最被选择的行
  • ANCHOR 指定的定位行
  • '@x, y' 参考点 (x, y) 为对应部件左上角的座标点,指最接近该点的行.

方法及说明#

方法 & 说明
activate(index)
选择某行
bbox(index)
返回某行所在的位置 (左,上,宽,高), 看不见则返回 None, 部份看见会全部返回
curselection()
返回目前所有的选项 tuple
delete(first, last=None)
删除第 first 行,或 first 到 last (含) 行
get(first, last=None)
返回最接近第 first 行的内容,或 first 到 last (含) 行的内容 tuple
index(i)
可能的话,第 i 行显示在可见的第一行
insert(index, *elements)
新增多行在 index 之前
itemcget(index, option)
返回第 index 行的该选项值,无则返回 None
itemconfig(index, option=value, …)
设置第 index 行的选项值,background (背景颜色)/foreground (前景颜色)/selectbackground (被选择文本的背景颜色)/selectforeground (被选择文本的前景颜色)
nearest(y)
返回最接近 y 座标,可见行的索引值
scan_dragto(x, y)
鼠标键按下事件处理程序中已标记位置时,鼠标滚动事件处理程序拖弋卷动部件,(x, y) 为位置
scan_mark(x, y)
鼠标键按下事件处理程序中标记位置 (x, y)
see(index)
调整列表,使第 index 可见
selection_anchor(index)
设置一个行引定位在 index, 以作为未来参考使用 ANCHOR
selection_clear(first, last=None)
去除 first (到含 last) 行的选择
selection_includes(index)
第 index 行是否已选
selection_set(first, last=None)
选择 first (到含 last) 行
size()
部件中行总数
xview()
供水平滚动条 scrollbar 选项 command 使用
xview_moveto(fraction)
移动水平滚动条,fraction 为 0 ~ 1
xview_scroll(number, what)
水平滚动画布 number 个单位,单位 what 为 UNITS 或 PAGES, UNITS 为字寛,PAGES 的大小为部件寛度,正值向右,负值向左
yview()
供垂直滚动条 scrollbar 选项 command 使用
yview_moveto(fraction)
移动垂直滚动条,fraction 为 0 ~ 1
yview_scroll(number, what)
水平滚动画布 number 个单位,单位 what 为 UNITS 或 PAGES, UNITS 为行,PAGES 的大小为部件高度,正值向下,负值向上

范例视窗及代码#

Tkinter (09) 列表框部件 Listbox

import tkinter as tk

root = tk.Tk()
root.wm_title("Listbox Demo")

font = ('Courier New', 16, 'bold')

elements = ('List for House', 'List for Friend', 'List for Cash', 'List for Car',
            'List for Motorcycle', 'List for Boat', 'List for Airplane')

x_scrollbar = tk.Scrollbar(orient=tk.HORIZONTAL)
x_scrollbar.grid(row=1, column=0, sticky=tk.E+tk.W)
y_scrollbar = tk.Scrollbar(orient=tk.VERTICAL)
y_scrollbar.grid(row=0, column=1, sticky=tk.N+tk.S)

list_box = tk.Listbox(root, font=font, selectmode=tk.MULTIPLE,
    width=15, height=5, activestyle='dotbox', xscrollcommand=x_scrollbar.set,
    yscrollcommand=y_scrollbar.set)
list_box.insert(tk.END, *elements)
list_box.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)

x_scrollbar['command'] = list_box.xview
y_scrollbar['command'] = list_box.yview

root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang