- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - Tkinter Listbox
The Listbox widget is used to display a list of items from which a user can select a number of items
Here is the simple syntax to create this widget −
w = Listbox ( master, option, ... )
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.
|Sr.No.||Option & Description|
The normal background color displayed behind the label and indicator.
The size of the border around the indicator. Default is 2 pixels.
The cursor that 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.
Number of lines (not pixels!) shown in the listbox. Default is 10.
Color 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 displaying 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, returns the text of the line closest to 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 new lines into the listbox before the line specified by 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 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 by lines, 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 −
# !/usr/bin/python3 from tkinter import * 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 −