效果圖:
點擊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()
常用參數列表:
一些常用的函數: