1.4K
The Listbox widget is used to display a list of items from which a user can select a number of items. In this article we will look at the syntax, parameters, functions, methods and some code examples
Syntax
Here is the simple syntax to create this widget −
w = Listbox ( master, option, ... )
Parameters
-
- master − This represents the parent window.
- options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
A list of possible options is given below.
Option | Description |
---|---|
bg | The background color of the widget. |
bd | It represents the size of the border. Default value is 2 pixel. |
cursor | The mouse pointer will look like the cursor type like dot, arrow, etc. |
font | The font type of the Listbox items. |
fg | The color of the text. |
height | It represents the count of the lines shown in the Listbox. The default value is 10. |
highlightcolor | The color of the Listbox items when the widget is under focus. |
highlightthickness | The thickness of the highlight. |
relief | The type of the border. The default is SUNKEN. |
selectbackground | The background color that is used to display the selected text. |
selectmode | It is used to determine the number of items that can be selected from the list. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED. |
width | It represents the width of the widget in characters. |
xscrollcommand | It is used to let the user scroll the Listbox horizontally. |
yscrollcommand | It is used to let the user scroll the Listbox vertically. |
Methods
There are the following methods associated with the Listbox.
Method | Description |
---|---|
activate(index) | This is used to select the lines at the specified index. |
curselection() | This returns a tuple containing the line numbers of the selected element or elements, counting from 0. If nothing is selected, returns an empty tuple. |
delete(first, last = None) | This is used to delete the lines which exist in the given range. |
get(first, last = None) | This is used to get the list items that exist in the given range. |
index(i) | This is used to place the line with the specified index at the top of the widget. |
insert(index, *elements) | This is used to insert the new lines with the specified number of elements before the specified index. |
nearest(y) | This returns the index of the nearest line to the y coordinate of the Listbox widget. |
see(index) | This is used to adjust the position of the listbox to make the lines specified by the index visible. |
size() | This returns the number of lines that are present in the Listbox widget. |
xview() | This is used to make the widget horizontally scrollable. |
xview_moveto(fraction) | This is used to make the listbox horizontally scrollable by the fraction of width of the longest line present in the listbox. |
xview_scroll(number, what) | This is used to make the listbox horizontally scrollable by the number of characters specified. |
yview() | This allows the Listbox to be vertically scrollable. |
yview_moveto(fraction) | This is used to make the listbox vertically scrollable by the fraction of width of the longest line present in the listbox. |
yview_scroll (number, what) | This is used to make the listbox vertically scrollable by the number of characters specified. |
Code examples
from Tkinter import * import tkMessageBox import Tkinter top = Tk() Lb1 = Listbox(top) Lb1.insert(1, "France") Lb1.insert(2, "Germany") Lb1.insert(3, "UK") Lb1.insert(4, "USA") Lb1.insert(5, "Canada") Lb1.insert(6, "Mexico") Lb1.pack() top.mainloop()
from tkinter import * top = Tk() top.title('Python Guides') top.geometry('400x300') var = StringVar() def showSelected(): countries = [] cname = lb.curselection() for i in cname: op = lb.get(i) countries.append(op) for val in countries: print(val) show = Label(top, text = "Select Your Country", font = ("Times", 12), padx = 5, pady = 5) show.pack() lb = Listbox(top, selectmode = "multiple") lb.pack(padx = 5, pady = 5, expand = YES, fill = "both") x =["Australia", "Brazil", "Canada","France","Germany", "Mexico", "UK", "USA"] for item in range(len(x)): lb.insert(END, x[item]) lb.itemconfig(item, bg="#bdc1d6") Button(top, text="Show Selected", command=showSelected).pack() top.mainloop()