TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Tkinter Listbox

Python Tkinter Listbox with python tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.

<< Back to PYTHON

Python Tkinter Listbox

The Listbox widget is used to display the list items to the user. We can place only text items in the Listbox and all text items contain the same font and color.

The user can choose one or more items from the list depending upon the configuration.

The syntax to use the Listbox is given below.

w = Listbox(parent, options) 

A list of possible options is given below.

SN Option Description
1 bg The background color of the widget.
2 bd It represents the size of the border. Default value is 2 pixel.
3 cursor The mouse pointer will look like the cursor type like dot, arrow, etc.
4 font The font type of the Listbox items.
5 fg The color of the text.
6 height It represents the count of the lines shown in the Listbox. The default value is 10.
7 highlightcolor The color of the Listbox items when the widget is under focus.
8 highlightthickness The thickness of the highlight.
9 relief The type of the border. The default is SUNKEN.
10 selectbackground The background color that is used to display the selected text.
11 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.
12 width It represents the width of the widget in characters.
13 xscrollcommand It is used to let the user scroll the Listbox horizontally.
14 yscrollcommand It is used to let the user scroll the Listbox vertically.

Methods

There are the following methods associated with the Listbox.

SN Method Description
1 activate(index) It is used to select the lines at the specified index.
2 curselection() It returns a tuple containing the line numbers of the selected element or elements, counting from 0. If nothing is selected, returns an empty tuple.
3 delete(first, last = None) It is used to delete the lines which exist in the given range.
4 get(first, last = None) It is used to get the list items that exist in the given range.
5 index(i) It is used to place the line with the specified index at the top of the widget.
6 insert(index, *elements) It is used to insert the new lines with the specified number of elements before the specified index.
7 nearest(y) It returns the index of the nearest line to the y coordinate of the Listbox widget.
8 see(index) It is used to adjust the position of the listbox to make the lines specified by the index visible.
9 size() It returns the number of lines that are present in the Listbox widget.
10 xview() This is used to make the widget horizontally scrollable.
11 xview_moveto(fraction) It is used to make the listbox horizontally scrollable by the fraction of width of the longest line present in the listbox.
12 xview_scroll(number, what) It is used to make the listbox horizontally scrollable by the number of characters specified.
13 yview() It allows the Listbox to be vertically scrollable.
14 yview_moveto(fraction) It is used to make the listbox vertically scrollable by the fraction of width of the longest line present in the listbox.
15 yview_scroll (number, what) It is used to make the listbox vertically scrollable by the number of characters specified.

Example 1

# !/usr/bin/python3

from tkinter import *

top = Tk()

top.geometry("200x250")

lbl = Label(top,text = "A list of favourite countries...")

listbox = Listbox(top)

listbox.insert(1,"India")

listbox.insert(2, "USA")

listbox.insert(3, "Japan")

listbox.insert(4, "Austrelia")

lbl.pack()
listbox.pack()

top.mainloop()

Output:

Python Tkinter Listbox

Example 2: Deleting the active items from the list

# !/usr/bin/python3

from tkinter import *

top = Tk()

top.geometry("200x250")

lbl = Label(top,text = "A list of favourite countries...")

listbox = Listbox(top)

listbox.insert(1,"India")

listbox.insert(2, "USA")

listbox.insert(3, "Japan")

listbox.insert(4, "Austrelia")

#this button will delete the selected item from the list 

btn = Button(top, text = "delete", command = lambda listbox=listbox: listbox.delete(ANCHOR))

lbl.pack()


listbox.pack()

btn.pack()
top.mainloop()

Output:

Python Tkinter Listbox

After pressing the delete button.

Python Tkinter Listbox



Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf