Python Tkinter Listbox is a widget in the Tkinter GUI toolkit that allows you to display a list of items to the user, and allows them to select one or more items from the list.
How To create a Listbox in Tkinter?
listbox = Listbox(parent, options...)
In this syntax, Listbox is the name of the widget, parent is the parent widget in which the Listbox will be placed, and options are the configuration options that can be used to customize the appearance and behavior of the Listbox widget.
Here’s an example that creates a simple Listbox widget:
from tkinter import * root = Tk() listbox = Listbox(root) listbox.insert(1, "Option 1") listbox.insert(2, "Option 2") listbox.insert(3, "Option 3") listbox.pack() root.mainloop()
In this example, we import the tkinter module and create the main window using the Tk() function. Then, we create a Listbox widget using the Listbox() function and add three options to it using the insert() method. Finally, we pack the Listbox widget and start the main event loop using the mainloop() method.
Here is a complete list of configuration options for the Python Tkinter Listbox widget:
activestyle: Sets the style of the active element in the Listbox (e.g., “DOTBOX”, “NONE”, “DASHED”, “SOLID”, or “UNDERLINE”).
bg: Sets the background color of the Listbox.
bd: Sets the border width of the Listbox.
cursor: Sets the cursor shape when it is over the Listbox.
disabledforeground: Sets the foreground color to use when the Listbox is disabled.
exportselection: Determines whether the current selection is exported to the clipboard.
fg: Sets the foreground color (text color) of the Listbox.
font: Sets the font used to display the items in the Listbox.
height: Sets the number of visible rows in the Listbox.
highlightbackground: Sets the color of the highlight border when the Listbox does not have focus.
highlightcolor: Sets the color of the highlight when an item is selected.
highlightthickness: Sets the thickness of the highlight border.
justify: Sets the alignment of the text within each item of the Listbox.
listvariable: Specifies the Tkinter variable that holds the contents of the Listbox.
relief: Sets the type of border used around the Listbox.
selectbackground: Sets the background color of selected items.
selectborderwidth: Sets the border width of selected items.
selectforeground: Sets the foreground color of selected items.
selectmode: Sets the mode of selection for the Listbox (e.g., “SINGLE”, “BROWSE”, “MULTIPLE”, or “EXTENDED”).
setgrid: Determines whether the Listbox items are aligned in a grid.
takefocus: Determines whether the Listbox can receive focus.
width: Sets the width of the Listbox in characters.
xscrollcommand: Specifies a scrollbar widget that controls the horizontal scrolling of the Listbox.
yscrollcommand: Specifies a scrollbar widget that controls the vertical scrolling of the Listbox.
You can set these options using the configure() method, which takes one or more keyword arguments.
For example, to set the background color of a Listbox to red, you can use the following code:
listbox.configure(bg="red")
It sets the style of the active element (the element currently selected by the user) in the listbox.
There are three possible values for activestyle:
none (default): The active element is not highlighted.
dotbox: The active element is highlighted by drawing a dotted rectangle around it.
underline: The active element is highlighted by underlining it.
Here’s an example of how to use the activestyle option:
from tkinter import * root = Tk() listbox = Listbox(root, activestyle="dotbox") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
To see the effect of option click on an item
This code creates a Listbox with active style set to “dotbox”, which means that the active element will be highlighted by drawing a dotted rectangle around it.
The bg option of the Python Tkinter Listbox widget sets the background color of the widget.
Here’s an example of how to use the bg option:
from tkinter import * root = Tk() listbox = Listbox(root, bg="red") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a red background. You can use any valid color name or color code as the value for bg.
The bd option of the Python Tkinter Listbox widget sets the width of the border around the widget.
Here’s an example of how to use the bd option:
from tkinter import * root = Tk() listbox = Listbox(root, bd=50) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a border width of 2 pixels. You can use any integer value for bd. If you set bd=0, the Listbox will have no border.
The cursor option of the Python Tkinter Listbox widget sets the type of cursor that is displayed when the mouse pointer is over the widget.
Here’s an example of how to use the cursor option:
from tkinter import * root = Tk() listbox = Listbox(root, cursor="plus") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a “plus” cursor. You can use any valid cursor name as the value for cursor. Some of the possible cursor names are “arrow”, “cross”, “fleur”, “hand1”, “hand2”, “question_arrow”, and “watch”. If you set cursor=””, the default cursor for the operating system will be used.
The disabledforeground option of the Python Tkinter Listbox widget sets the color of the text when the Listbox is disabled (not interactable).
Here’s an example of how to use the disabledforeground option:
from tkinter import * root = Tk() listbox = Listbox(root, disabledforeground="blue") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") # disable the Listbox listbox.config(state="disabled") listbox.pack() root.mainloop()
This code creates a Listbox with a disabledforeground color of “gray”. When the Listbox is disabled using the config(state=”disabled”) method, the text color will be changed to the disabledforeground color. If you don’t set a value for disabledforeground, the default color for disabled widgets will be used.
The exportselection option of the Python Tkinter Listbox widget controls whether or not the selected item in the Listbox is automatically copied to the clipboard.
Here’s an example of how to use the exportselection option:
from tkinter import * root = Tk() listbox = Listbox(root, exportselection=False) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with the exportselection option set to False. This means that the selected item in the Listbox will not be automatically copied to the clipboard. If you set exportselection=True, the selected item will be copied to the clipboard.
The fg option of the Python Tkinter Listbox widget sets the foreground color of the text in the widget.
Here’s an example of how to use the fg option:
from tkinter import * root = Tk() listbox = Listbox(root, fg="blue") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with blue text. You can use any valid color name or color code as the value for fg. If you don’t set a value for fg, the default text color for the operating system will be used.
The font option of the Python Tkinter Listbox widget sets the font of the text in the widget.
Here’s an example of how to use the font option:
from tkinter import * root = Tk() listbox = Listbox(root, font=("Arial", 12)) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with text using the Arial font with a size of 12 points. You can use any valid font name and size as the value for font. If you don’t set a value for font, the default font for the operating system will be used.
The height option of the Python Tkinter Listbox widget sets the number of lines that are visible in the Listbox at once.
Here’s an example of how to use the height option:
from tkinter import * root = Tk() listbox = Listbox(root, height=3) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.insert(4, "Item 4") listbox.insert(5, "Item 5") listbox.insert(6, "Item 6") listbox.pack() root.mainloop()
This code creates a Listbox with a height of 3, which means that three lines of items will be visible at once. If there are more items than can fit in the visible area, the Listbox will display a scrollbar to allow the user to scroll through the list. You can set any integer value as the value for height. If you don’t set a value for height, the Listbox will use a default height that fits all items.
The highlightbackground option of the Python Tkinter Listbox widget sets the color of the border that is displayed around the Listbox when it does not have the focus.
Here’s an example of how to use the highlightbackground option:
from tkinter import * root = Tk() listbox = Listbox(root, highlightbackground="red") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a highlightbackground color of “red”. When the Listbox does not have the focus, a red border will be displayed around it. If you don’t set a value for highlightbackground, the default highlight color for the operating system will be used.
The highlightcolor option of the Python Tkinter Listbox widget sets the color of the border that is displayed around the Listbox when it has the focus.
Here’s an example of how to use the highlightcolor option:
from tkinter import * root = Tk() listbox = Listbox(root, highlightcolor="blue") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a highlightcolor of “blue”. When the Listbox has the focus, a blue border will be displayed around it. If you don’t set a value for highlightcolor, the default highlight color for the operating system will be used.
The highlightthickness option of the Python Tkinter Listbox widget sets the thickness of the highlight border that is displayed around the Listbox when it has the focus or does not have the focus.
Here’s an example of how to use the highlightthickness option:
from tkinter import * root = Tk() listbox = Listbox(root, highlightthickness=2) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a highlightthickness of 2 pixels. When the Listbox has the focus or does not have the focus, a highlight border with a thickness of 2 pixels will be displayed around it. If you don’t set a value for highlightthickness, the default highlight thickness for the operating system will be used.
The listvariable option of the Python Tkinter Listbox widget links the contents of the Listbox to a Tkinter variable, which can be a StringVar, IntVar, DoubleVar, or BooleanVar. This allows you to update the Listbox contents dynamically by updating the variable value.
Here’s an example of how to use the listvariable option:
from tkinter import * root = Tk() var = StringVar() var.set("Item 1\nItem 2\nItem 3") listbox = Listbox(root, listvariable=var) listbox.pack() root.mainloop()
This code creates a StringVar variable var and sets its value to a string containing three items separated by newline characters. The listvariable option of the Listbox is set to var, so the contents of the Listbox will be the items in var. If you update the value of var later, the contents of the Listbox will automatically be updated to reflect the new value. Note that if you use the listvariable option, you should not use the insert method to add items to the Listbox. Instead, you should update the value of the variable that is linked to the Listbox.
The relief option of the Python Tkinter Listbox widget sets the 3D appearance of the Listbox border.
Here’s an example of how to use the relief option:
from tkinter import * root = Tk() listbox = Listbox(root, relief="solid") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a relief value of “solid”. The Listbox border will have a solid appearance, without any 3D effects. You can set the value of relief to “flat”, “raised”, “sunken”, “ridge”, or “groove” to give the border a different 3D appearance. If you don’t set a value for relief, the default value of “sunken” will be used.
The selectbackground option of the Python Tkinter Listbox widget sets the background color of the Listbox when an item is selected.
Here’s an example of how to use the selectbackground option:
from tkinter import * root = Tk() listbox = Listbox(root, selectbackground="yellow") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a selectbackground value of “yellow”. When an item is selected, the background color of the item will be changed to yellow. You can set the value of selectbackground to any valid color name or color code to change the color of the selected item’s background. If you don’t set a value for selectbackground, the default value of the system’s default selection background color will be used.
The selectborderwidth option of the Python Tkinter Listbox widget sets the width of the border around the selected item in the Listbox.
Here’s an example of how to use the selectborderwidth option:
from tkinter import * root = Tk() listbox = Listbox(root, selectborderwidth=5) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a selectborderwidth value of 5. When an item is selected, a border with a width of 5 pixels will be drawn around the item. You can set the value of selectborderwidth to any integer value to change the width of the border around the selected item. If you don’t set a value for selectborderwidth, the default value of 0 will be used, which means no border will be drawn around the selected item.
The selectforeground option of the Python Tkinter Listbox widget sets the text color of the selected item in the Listbox.
Here’s an example of how to use the selectforeground option:
from tkinter import * root = Tk() listbox = Listbox(root, selectforeground="blue") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a selectforeground value of “blue”. When an item is selected, the text color of the item will be changed to white. You can set the value of selectforeground to any valid color name or color code to change the color of the selected item’s text. If you don’t set a value for selectforeground, the default value of the system’s default selection foreground color will be used.
The selectmode option of the Python Tkinter Listbox widget sets the mode of selection in the Listbox. It determines how many items can be selected at once.
Here are the different values that can be set for selectmode:
“browse”: Only one item can be selected at a time. This is the default value.
“single”: Only one item can be selected at a time. Similar to “browse”.
“multiple”: Multiple items can be selected at a time. You can select multiple items by holding down the Ctrl key while clicking on the items.
“extended”: Multiple items can be selected at a time. You can select multiple items by clicking on the first item, holding down the mouse button, and dragging the mouse to select the other items.
Here’s an example of how to use the selectmode option:
from tkinter import * root = Tk() listbox = Listbox(root, selectmode="multiple") listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.insert(4, "Item 4") listbox.pack() root.mainloop()
This code creates a Listbox with a selectmode value of “multiple”. You can select multiple items in the Listbox by holding down the Ctrl key while clicking on the items. If you don’t set a value for selectmode, the default value of “browse” will be used.
The setgrid option of the Python Tkinter Listbox widget determines whether or not to use a grid layout for the Listbox.
Here’s an example of how to use the setgrid option:
from tkinter import * root = Tk() listbox = Listbox(root, setgrid=True) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a setgrid value of True. This means that the items in the Listbox will be displayed in a grid layout. If you set the value of setgrid to False, the items will be displayed in a single column layout. If you don’t set a value for setgrid, the default value of False will be used, and the items will be displayed in a single column layout.
The takefocus option of the Python Tkinter Listbox widget determines whether or not the Listbox can be given the focus.
Here’s an example of how to use the takefocus option:
from tkinter import * root = Tk() listbox = Listbox(root, takefocus=False) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a takefocus value of False. This means that the Listbox cannot be given the focus by clicking on it or using the Tab key to cycle through widgets. If you set the value of takefocus to True, the Listbox can be given the focus. If you don’t set a value for takefocus, the default value of True will be used, and the Listbox can be given the focus.
The width option of the Python Tkinter Listbox widget sets the width of the Listbox in characters.
Here’s an example of how to use the width option:
from tkinter import * root = Tk() listbox = Listbox(root, width=12345678910111213141516171819202120) listbox.insert(1, "Item 1") listbox.insert(2, "Item 2") listbox.insert(3, "Item 3") listbox.pack() root.mainloop()
This code creates a Listbox with a width of 20 characters. This means that the Listbox will be 20 characters wide, and any text that is longer than 20 characters will be truncated. If you don’t set a value for width, the Listbox will automatically resize itself to fit the width of its contents.
The xscrollcommand option of the Python Tkinter Listbox widget is used to connect a horizontal scrollbar to the Listbox.
Here’s an example of how to use the xscrollcommand option:
from tkinter import * root = Tk() scrollbar = Scrollbar(root, orient=HORIZONTAL) scrollbar.pack(side=BOTTOM, fill=X) listbox = Listbox(root, xscrollcommand=scrollbar.set) for i in range(1, 51): listbox.insert(END, "Item " + str(i)) listbox.pack(side=LEFT, fill=BOTH, expand=True) scrollbar.config(command=listbox.xview) root.mainloop()
In this example, a horizontal scrollbar is created using the Scrollbar widget and placed at the bottom of the window using the side and fill options. The Listbox is created with the xscrollcommand option set to the set method of the scrollbar.
The for loop is used to insert 50 items into the Listbox. The pack method is used to place the Listbox on the left side of the window, and the fill and expand options are used to make the Listbox fill the available space.
Finally, the config method is called on the scrollbar to set its command option to the xview method of the Listbox. This connects the scrollbar to the Listbox, allowing the user to scroll horizontally through the Listbox using the scrollbar.
The yscrollcommand option of the Python Tkinter Listbox widget is used to connect a vertical scrollbar to the Listbox.
Here’s an example of how to use the yscrollcommand option:
from tkinter import * root = Tk() scrollbar = Scrollbar(root) scrollbar.pack(side=RIGHT, fill=Y) listbox = Listbox(root, yscrollcommand=scrollbar.set) for i in range(1, 51): listbox.insert(END, "Item " + str(i)) listbox.pack(side=LEFT, fill=BOTH, expand=True) scrollbar.config(command=listbox.yview) root.mainloop()
In this example, a vertical scrollbar is created using the Scrollbar widget and placed on the right side of the window using the side and fill options. The Listbox is created with the yscrollcommand option set to the set method of the scrollbar.
The for loop is used to insert 50 items into the Listbox. The pack method is used to place the Listbox on the left side of the window, and the fill and expand options are used to make the Listbox fill the available space.
Finally, the config method is called on the scrollbar to set its command option to the yview method of the Listbox. This connects the scrollbar to the Listbox, allowing the user to scroll vertically through the Listbox using the scrollbar.