组件描述
当选项过多地时候,我们会使用ListBox选项框。
程序实现
代码
import tkinter as tk
root = tk.Tk()
# 创建一个选项框
my_listbox = tk.Listbox(root)
my_listbox.pack()
# 在选项框中插入选项
for item in ['一诺', '六点六', '笑影', '九年', '爱思', '老帅', '梦泪', '莲', '杰斯']:
my_listbox.insert(tk.END, item)
# 创建按钮,用于删除选中的选项
button1 = tk.Button(root, text='删除',
command=lambda x=my_listbox:x.delete(tk.ACTIVE)) # tk.ACTIVE表示当前选中的哪一项
button1.pack()
tk.mainloop()
显示效果
ListBox组件API
参数 | 作用 |
---|---|
frame | 组件放置窗口名称 |
height | 最多显示多少项选项(默认10项) |
yscrollcommand | 需要在Y轴方向滚动时触发的动作 |