โดยทั่วไปแล้ววิดเจ็ต Tkinter Listbox จะใช้เพื่อสร้างรายการ สามารถจัดเก็บรายการตัวเลข อักขระ และรองรับคุณสมบัติมากมาย เช่น การเลือกและแก้ไขรายการ
ในการแก้ไขรายการกล่องรายการ อันดับแรก เราต้องเลือกรายการในลูปโดยใช้ listbox.curselection() ฟังก์ชันและแทรกรายการใหม่หลังจากลบรายการก่อนหน้าในกล่องรายการ ในการแทรกรายการใหม่ในกล่องรายการ คุณสามารถใช้ listbox.insert(**items) ฟังก์ชัน
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้างรายการในวิดเจ็ตกล่องรายการ และปุ่มจะใช้สำหรับแก้ไขรายการที่เลือกในรายการ
# Import the required libraries
from tkinter import *
from tkinter import ttk
# 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, background="purple2", foreground="white", font=('Times 13'), selectbackground="black")
lb.pack()
# Select the list item and delete the item first
# Once the list item is deleted,
# we can insert a new item in the listbox
def edit():
for item in lb.curselection():
lb.delete(item)
lb.insert("end", "foo")
# Add items in the Listbox
lb.insert("end", "item1", "item2", "item3", "item4", "item5")
# Add a Button To Edit and Delete the Listbox Item
ttk.Button(win, text="Edit", command=edit).pack()
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะทำให้คุณสามารถเลือกและแก้ไขรายการได้

คุณสามารถกำหนดค่ารายการโดยคลิกปุ่ม "แก้ไข"
