Tkinter Widgets-Listbox

Listbox widgets are used to display list of options to the user and select one option to perform a function, according to the selection.
Syntax-

w = Listbox ( main,option)

Parameter-

Main- parent Window.
Option- attribute of button widget.

Example-
  1. from tkinter import *  
  2. import tkinter  
  3. top = Tk()  
  4. Lb1 = Listbox(top)  
  5. Lb1.insert(1"Python")  
  6. Lb1.insert(2"Perl")  
  7. Lb1.insert(3"C")  
  8. Lb1.insert(4"PHP")  
  9. Lb1.insert(5"JSP")  
  10. Lb1.insert(6"Ruby")  
  11. Lb1.pack()  
  12. top.mainloop()  
Output-


Option
Description
bg
Normal background color. 
bdSets Border width in pixels. Default is 2.
cursor
Cursor is used to create a cursor in canvas like arrow, circle, dot etc.
font It is used to set font of the text.
fg
It sets normal foreground (text) color of widgets.
height
It is used to set the height of widgets.
highlightcolorIt changes the color of widgets on focus.
highlightthickness
It is used to set thickness of the focus highlight.
relief
Relief is special type of border. The values are SUNKEN, RAISED, GROOVE, and RIDGE. 
selectbackground
It is used to set the background color of the selected text.
selectmode
It is used to set the selection mode like BROWSE,SINGLE,MULTIPLE,EXTENDED.
width
It is used to set the width of widgets.
xscrollcommand
It is used to scroll on the defined value in x direction.
yscrollcommand
It is used to define the scrolling value in y direction
Summary
In this chapter, you learnt what listbox widget is and how to use the property of the widget.
Next » Tkinter Widget-MenuButton