The Listbox widget is utilized to exhibit a list of keys from which the users could choose several items. Python Tkinter Listbox is adopted to display the inventory items to the consumer. We could put only text objects in the Listbox and entire text items comprise the similar font and color. The user could select one or additional items from the inventory depending upon the configuration.
Here is the simple syntax to create this widget:
w = Listbox ( master, option, ... )
|Sr.No.||Option & Description|
The normal background color is displayed behind the label and indicator.
The size of the border around the indicator. Default is 2 pixels.
The cursor appears when the mouse is over the Listbox.
The font used for the text in the Listbox.
The color used for the text in the Listbox.
A number of lines (not pixels!) is shown in the Listbox. Default is 10.
Color is shown in the focus highlight when the widget has the focus.
Thickness of the focus highlight.
Selects three-dimensional border shading effects. The default is SUNKEN.
The background color to use displays selected text.
Determines how many items can be selected, and how mouse drags affect the selection -
The width of the widget in characters. The default is 20.
If you want to allow the user to scroll the Listbox horizontally, you can link your Listbox widget to a horizontal scrollbar.
If you want to allow the user to scroll the Listbox vertically, you can link your Listbox widget to a vertical scrollbar.
Methods on Listbox objects include -
|Sr.No.||Option & Description|
activate ( index )
Selects the line specifies by the given index.
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 )
Deletes the lines whose indices are in the range [first, last]. If the second argument is omitted, the single line with index first is deleted.
get ( first, last=None )
Returns a tuple containing the text of the lines with indices from first to last, inclusive. If the second argument is omitted, return the text of the line closest to the first.
index ( i )
If possible, positions the visible part of the Listbox so that the line containing index i is at the top of the widget.
insert ( index, *elements )
Insert one or more newlines into the Listbox before the line specified by the index. Use END as the first argument if you want to add new lines to the end of the Listbox.
nearest ( y )
Return the index of the visible line closest to the y-coordinate y relative to the Listbox widget.
see ( index )
Adjust the position of the Listbox so that the line referred to by the index is visible.
Returns the number of lines in the Listbox.
To make the Listbox horizontally scrollable, set the command option of the associated horizontal scrollbar to this method.
xview_moveto ( fraction )
Scroll the Listbox so that the leftmost fraction of the width of its longest line is outside the left side of the Listbox. Fraction is in the range [0,1].
xview_scroll ( number, what )
Scrolls the Listbox horizontally. For the what argument, use either UNITS to scroll by characters, or PAGES to scroll by pages, that is, by the width of the Listbox. The number argument tells how many to scroll.
To make the Listbox vertically scrollable, set the command option of the associated vertical scrollbar to this method.
yview_moveto ( fraction )
Scroll the Listbox so that the top fraction of the width of its longest line is outside the left side of the Listbox. Fraction is in the range [0,1].
yview_scroll ( number, what )
Scrolls the Listbox vertically. For the what argument, use either UNITS to scroll bylines, or PAGES to scroll by pages, that is, by the height of the Listbox. The number argument tells how many to scroll.
Try the following example yourself
If you need an idea for a Christmas present then think about the Rubik's Cube which is the best-selling puzzle toy in the World.
from Tkinter import * import tkMessageBox import Tkinter top = Tk() Lb1 = Listbox(top) Lb1.insert(1, "Python") Lb1.insert(2, "Perl") Lb1.insert(3, "C") Lb1.insert(4, "PHP") Lb1.insert(5, "JSP") Lb1.insert(6, "Ruby") Lb1.pack() top.mainloop()
When the above code is executed, it produces the following result:
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.