Listbox()
Let us take a look at an example of this.
# Import the Tkinter library
from tkinter import *
# Create an instance of Tkinter window
window=Tk()
# Set the size of the window
window.geometry("300x300")
# Create a label widget
label=Label(window, text="List of fruits: ").pack()
# Create a Listbox widget
listbox = Listbox(window)
# Add elemnets to list
listbox.insert(1,"Apple")
listbox.insert(2,"Orange")
listbox.insert(3,"Banana")
listbox.pack()
window.mainloop()Note: The above program runs on the Tkinter version
8.6
In the above code snippet,
Line 5: We create an instance of the Tkinter class, Tk(), and assign it to variable, window.
Line 8: We use the geometry method to set the window size as 300x300 px.
Line 11: We use the Label() widget to create a label.
Line 14: We use the Listbox() widget to create a list and attach it to the window.
Line 17, 18 & 19: We use the insert method to insert elements in the listbox. The insert method takes the position as the first parameter and the value as the second parameter.
Free Resources