Python Tkinter Listbox

Created with Sketch.

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.

  1. w = Listbox(parent, options)

A list of possible options is given below.

SNOptionDescription
1bgThe background color of the widget.
2bdIt represents the size of the border. Default value is 2 pixel.
3cursorThe mouse pointer will look like the cursor type like dot, arrow, etc.
4fontThe font type of the Listbox items.
5fgThe color of the text.
6heightIt represents the count of the lines shown in the Listbox. The default value is 10.
7highlightcolorThe color of the Listbox items when the widget is under focus.
8highlightthicknessThe thickness of the highlight.
9reliefThe type of the border. The default is SUNKEN.
10selectbackgroundThe background color that is used to display the selected text.
11selectmodeIt is used to determine the number of items that can be selected from the list. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.
12widthIt represents the width of the widget in characters.
13xscrollcommandIt is used to let the user scroll the Listbox horizontally.
14yscrollcommandIt is used to let the user scroll the Listbox vertically.

Methods

There are the following methods associated with the Listbox.

SNMethodDescription
1activate(index)It is used to select the lines at the specified index.
2curselection()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.
3delete(first, last = None)It is used to delete the lines which exist in the given range.
4get(first, last = None)It is used to get the list items that exist in the given range.
5index(i)It is used to place the line with the specified index at the top of the widget.
6insert(index, *elements)It is used to insert the new lines with the specified number of elements before the specified index.
7nearest(y)It returns the index of the nearest line to the y coordinate of the Listbox widget.
8see(index)It is used to adjust the position of the listbox to make the lines specified by the index visible.
9size()It returns the number of lines that are present in the Listbox widget.
10xview()This is used to make the widget horizontally scrollable.
11xview_moveto(fraction)It is used to make the listbox horizontally scrollable by the fraction of width of the longest line present in the listbox.
12xview_scroll(number, what)It is used to make the listbox horizontally scrollable by the number of characters specified.
13yview()It allows the Listbox to be vertically scrollable.
14yview_moveto(fraction)It is used to make the listbox vertically scrollable by the fraction of width of the longest line present in the listbox.
15yview_scroll (number, what)It is used to make the listbox vertically scrollable by the number of characters specified.

Example 1

  1. # !/usr/bin/python3
  2. from tkinter import *
  3. top = Tk()
  4. top.geometry(“200×250”)
  5. lbl = Label(top,text = “A list of favourite countries…”)
  6. listbox = Listbox(top)
  7. listbox.insert(1,“India”)
  8. listbox.insert(2“USA”)
  9. listbox.insert(3“Japan”)
  10. listbox.insert(4“Austrelia”)
  11. lbl.pack()
  12. listbox.pack()
  13. top.mainloop()

Output:

Python Tkinter Listbox

Example 2: Deleting the active items from the list

  1. # !/usr/bin/python3
  2. from tkinter import *
  3. top = Tk()
  4. top.geometry(“200×250”)
  5. lbl = Label(top,text = “A list of favourite countries…”)
  6. listbox = Listbox(top)
  7. listbox.insert(1,“India”)
  8. listbox.insert(2“USA”)
  9. listbox.insert(3“Japan”)
  10. listbox.insert(4“Austrelia”)
  11. #this button will delete the selected item from the list 
  12. btn = Button(top, text = “delete”, command = lambda listbox=listbox: listbox.delete(ANCHOR))
  13. lbl.pack()
  14. listbox.pack()
  15. btn.pack()
  16. top.mainloop()

Output:

Python Tkinter Listbox

After pressing the delete button.

Python Tkinter Listbox

 

Leave a Reply

Your email address will not be published. Required fields are marked *