The ListBox widget is used to display different types of items . These items must be of the same font type and have the same font color. Items must also be of type Text. The user can select one or more items from the list provided, according to the requirement.
Syntax
listbox = Listbox(root, bg, fg, bd, height, width, font, ..)
from tkinter import *
from tkinter.ttk import *
window = Tk()
window.title("Listbox")
window.geometry( '200x200')
lb = Listbox(window, height = 3)
lb.grid (row = 0, column = 0)
window.mainloop()
Python Tkinter - How to insert item into ListBox
When you create a listbox, it is empty. The first thing to do is usually to insert one or more lines of text. For this, the insert method (index, item) is used.
The insert method uses an index and a string to insert. The index is usually an item number (0 for the first item in the list), but you can also use some special indexes, including ACTIVE, which refers to the "active" item (defined when you click on an item or by the arrow) and END, which is used to attach items to the list.
from tkinter import *
from tkinter.ttk import *
window = Tk()
window.title("Listbox")
window.geometry('220x200')
lb = Listbox(window, height = 8)
lb.grid(row = 0, column = 0)
# Adding an element to the listbox
lb.insert(1, 'PHP')
lb.insert(2, 'Python')
lb.insert(3, 'MySQL')
window.mainloop()
Now, suppose you have a list of elements and I would like to insert them in the list box, without having to insert them this way, which is one by one, for that, we will simply use the for loop and the insert method has another property called END, which allows inserting elements at the end of the listbox without having to specify the index.
from tkinter import *
from tkinter.ttk import *
window = Tk()
window.title("Listbox")
window.geometry('210x200')
lb = Listbox(window, height = 6)
lb.grid(row = 0, column = 0)
# Adding an element to the listbox
lb.insert(1, 'PHP')
lb.insert(2, 'Python')
lb.insert(3, 'MySQL')
items = ["JS", "Java", "C ++"]
for i in items:
lb.insert(END, i)
window.mainloop()
Python Tkinter - How to get items from Tkinter Listbox
In addition to the insert method, the listbox has other methods, such as get () , to get the list items in a given interval.
Example:
# prints only the firstelement
print(lb.get(ACTIVE))
# prints only the elements in that interval
print(lb.get(1,2))
As a more practical example, we will create a function that reads and prints the selected element in LISTBOX, each time we click the button.
def test():
# prints only the first element
print(lb.get (ACTIVE))
l = Label(window, text = lb.get (ACTIVE), width = 10)
l.grid (row = 0, column = 1 )
b = Button (window, text = 'print', width = 10, command = test)
b.grid (row = 1, column = 0) We
We can also Read the selected element by clicking, using the curseselection() method - returns a tuple for all the line numbers being selected.
def test(event):
# prints only the first element
index = int(lb.curselection()[0])
value = lb.get(index)
l = Label(window, text = "You selected item" +
str(index ) + "" + str(value), width = 50)
l.grid(row = 0, column = 1)
lb.bind('<<ListboxSelect>>', test)
Python Tkinter - How to delete elements from Tkinter ListBox
To exclude elements from a Listbox, use if the methods listed below:
delete(start, last) # Delete lines in the specified range.
delete(ANCHOR) # Delete selected element
delete(2) # Delete the second element from the
delete(0, END) # Delete all elements
Post a Comment
0Comments