Tkinter ListBox insert() Method
You can use the insert() method to create items for the list box, and the format of this method is as follows.
insert(index, elements)
The index above is the item insertion position, if it is inserted at the end you can use END.
Tkinter ListBox insert() Method Example#1
Create a list box, and also create Banana, Watermelon, and Pineapple items for this list box.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root)
lb.insert(END,"Banana")
lb.insert(END,"Watermelon")
lb.insert(END,"Pineapple")
lb.pack(pady=10)
root.mainloop()
Output:
However, if there are many items to be created, it is recommended to use the list method to store the items first, and then use the for…in loop to insert the list items into the list box.
Tkinter ListBox insert() Method Example#2
Create a list box with 6 items. Lines 3 and 4 of the program create the list of fruits, and lines 12 and 13 insert the list elements into the list box respectively.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
root.mainloop()
Output:
Tkinter ListBox insert() Method Example#3
Add the selectmode=MULTIPLE parameter setting when using the Listbox() constructor, this setting allows the user to select multiple items.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root,selectmode=MULTIPLE)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
root.mainloop()
Output:
Tkinter ListBox insert() Method Example#4
Using the selectmode=EXTENDED parameter, you can then select interval items by dragging and dropping them. If you click on an item first, then hold down the Shift key and click on another item you can select the items in that interval.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root,selectmode=EXTENDED)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
root.mainloop()
Output:
Currently insert options are inserted at the end, so the syntax is insert (END, elements), in fact, the first parameter is the index value, if the END is changed to ACTIVE, that is, in front of the current option to add an item, if the option has not been selected then this ACTIVE is 0.
Tkinter ListBox insert() Method Example#5
Create three options first, then use insert (ACTIVE,elements …) to create another three options in front of the current option.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root,selectmode=EXTENDED)
for fruit in fruits:
lb.insert(END,fruit)
lb.insert(ACTIVE,"Orange","Grapes","Mango")
lb.pack(pady=10)
root.mainloop()
Output:
Readers should note the way three items are inserted at once in line 13.