Listbox
列表框
组件用于显示一个选择列表
Listbox 只能包含文本项目,所有项目的字体和颜色都要相同。根据组件的配置,用户可以从列表中选择一个或多个选项。
import tkinter as tk
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)
def check():
print(theLB.curselection())
if theLB.curselection():
param = theLB.get(theLB.curselection())
print("选择:", param)
tk.Button(window, text="获取值", command=check).pack()
tk.Button(window, text="删除值", command=lambda x=theLB: x.delete("active")).pack()
window.mainloop()