Tkinter(三):Listbox 列表部件
效果图:
点击print selection按钮,将Listbox中,选中的值,显示到Label上
import tkinter as tk # 定义窗口 window = tk.Tk() window.title('my window') # 窗口title window.geometry('350x300') # 窗口尺寸 # 定义Label varText1 = tk.StringVar() l = tk.Label(window, bg="yellow", width=20, textvariable=varText1) l.pack() # 定义Listbox varText2 = tk.StringVar() # 初始化值 varText2.set((11, 22, 33, 44, 55, 66, 77, 88, 99)) lb = tk.Listbox(window, listvariable=varText2) # 列表循环插入 list_item = ["aa", "bb", "cc", "dd"] for item in list_item: lb.insert("end", item) # 索引插入 lb.insert(0, "zero") lb.insert(1, "first") lb.insert(2, "seconed") lb.pack() def print_selection(): # 获取Listbox中光标选中的值 value = lb.get(lb.curselection()) varText1.set(value) # 定义button b1 = tk.Button(window, text='print selection', width=20, height=1, command=print_selection) b1.pack(pady=10) window.mainloop()
常用参数列表:
一些常用的函数:
我走的很慢,但从不后退