Write
Python GUI program to add items in listbox widget and to print and delete the
selected items from listbox on button click. Provide three separate buttons to
add, print and delete.
import
tkinter as tk
def
add_item():
item = entry.get()
if item:
listbox.insert(tk.END, item)
entry.delete(0, tk.END)
def
print_item():
selected_index = listbox.curselection()
if selected_index:
selected_item =
listbox.get(selected_index[0])
print("Selected item:", selected_item)
def
delete_item():
selected_index = listbox.curselection()
if selected_index:
listbox.delete(selected_index[0])
# Create the
main window
root =
tk.Tk()
root.title("Listbox
Example")
# Entry
widget for adding items
entry = tk.Entry(root)
entry.pack()
# Buttons to
add, print, and delete items
add_button =
tk.Button(root, text="Add", command=add_item)
print_button
= tk.Button(root, text="Print", command=print_item)
delete_button
= tk.Button(root, text="Delete", command=delete_item)
add_button.pack()
print_button.pack()
delete_button.pack()
# Listbox to
display items
listbox =
tk.Listbox(root)
listbox.pack()
root.mainloop()