เราใช้วิดเจ็ต Tkinter Listbox เพื่อสร้างรายการ แต่ละรายการในกล่องรายการมีดัชนีบางรายการที่กำหนดตามลำดับในแนวตั้ง
สมมติว่าเราต้องการรับดัชนีของรายการที่คลิกในกล่องรายการ จากนั้น เราต้องสร้างปุ่มที่จะจับการเลือกปัจจุบันของรายการโดยใช้ list.curselection() เมธอด จากนั้นเราจะพิมพ์ดัชนีโดยใช้ get() วิธีการ
ตัวอย่าง
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Create a Listbox widget
lb = Listbox(win, width=100, height=10, font=('Times 13'), selectbackground="black")
lb.pack()
# Define a function to edit the listbox ite
def save():
for item in lb.curselection():
print("You have selected " + str(item+1))
# Add items in the Listbox
lb.insert("end", "A", "B", "C", "D", "E", "F")
# Add a Button To Edit and Delete the Listbox Item
Button(win, text="Save", command=save).pack()
win.mainloop() ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น จะแสดงหน้าต่างที่มีรายการตัวอักษร (A-F)

เลือกรายการจากรายการและคลิกปุ่ม "บันทึก" เพื่อรับดัชนีของรายการที่เลือกพิมพ์บนคอนโซล
You have selected 3