import tkinter as tkwindow = tk.Tk()# 设置窗口window.title('my window')window.geometry('300x200')# 标签值用到的变量titleLV = tk.StringVar()# 添加一个标签l = tk.Label(window, bg='yellow', width=4, textvariable=titleLV)l.pack()# 获取选中的值并填入到标签def print_selection(): # 获取选中的值 var = lb.get(lb.curselection()) titleLV.set(var)b = tk.Button(window, { 'text': 'print selection', 'width': 15, 'height': 2, 'command': print_selection})b.pack()var2 = tk.StringVar()# 设置列表数据var2.set((11, 22, 33, 44))lb = tk.Listbox(window, { 'listvariable': var2})list_items = [1, 2, 3, 4]# 在列表中插入数据for item in list_items: # 向后插入 lb.insert('end', item)# 按照索引插入lb.insert(1, '在位置 1 插入')lb.insert(2, '在位置 2 插入')# 删除索引为 2 的数据lb.delete(2)lb.pack()window.mainloop()