Best post- Listbox and Scrollbar(Slider) Widget-Python

Listbox and Scrollbar (Slider) are Widgets of tkinter in Python

Tkinter provides many widgets for GUI in Python.

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. We can select one or more items together depending on the options used.
Scrollbar or slider widget provides a slide controller that is used to implement vertical scrolled widgets, such as Listbox, Text and Canvas. It can also create horizontal scrollbars on Entry widgets.

Listbox Widget

The Listbox widget is used to display a list of items from which a user can select a number of items.
When you first create the listbox, it is empty. The first thing to do is usually to insert one or more lines of text.
The insert method takes an index and a string to insert.
The index is usually an item number (0 for the first item in the list), but you can also use some special indexes, including ACTIVE, which refers to the “active” item (set when you click on an item, or by the arrow keys), and END, which is used to append items to the list.

Syntax
Here is the simple syntax to create this widget −

w = Listbox( master, option=value, ... )

Parameters
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.

Various options of Listbox widget:

  • bg : The normal background color displayed behind the label and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • cursor : cursor that will appear once the mouse is over the text box
  • fg : This option specifies the color of the text. If you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the bitmap.
  • height : The vertical dimension of the new frame.
  • width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.
  • state : The default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
  • selectbackground : Background colour once element is selected.
  • selectforeground : Foreground colour once element is selected.
  • selectmode : It can be SINGLE, BROWSE, MULTIPLE, EXTENDED
  • highlightcolor : Border colour when the Listbox is in focus.
  • highlightthickness : Thickness of selected Listbox border

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :30th March 2020

Description : Program to show use of Listbox widget

"""
from tkinter import *
root = Tk()
itv = Listbox(root,width = 35, bg='black', fg='orange',font='Verdana',
              selectbackground='yellow',selectforeground='green',
          selectmode='multiple',highlightcolor='blue',highlightthickness=5)
itv.pack()
itv.delete(0, END) 
itv.insert(END, "ITVoyagers has this row number as 1")
for value in range(2,11):
    itv.insert(END, "ITVoyagers has this row number as " + str(value))

OUTPUT

Output of Listbox widget in tkinter (itvoyagers.in)

Scrollbar (Slider) Widget

This widget provides a slide controller that is used to implement vertical scrolled widgets, such as Listbox, Text and Canvas. Note that you can also create horizontal scrollbars on Entry widgets.
To connect a vertical scrollbar to such a widget, you have to do two things:

  • Set the widget’s yscrollcommand callbacks to the set method of the scrollbar.
  • Set the scrollbar’s command to the yview method of the widget.

Syntax
Here is the simple syntax to create this widget −

w = Scrollbar( master, option=value, ... )

Parameters
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.

Various options of Scrollbar (Slider) widget:

  • activebackground : Background color when the button is under the cursor.
  • anchor: This option controls where the text is positioned. The default anchor is CENTER.Other possible values are NW(North West is top left), N, NE(North East is top right), W, E, SW(South West is bottom left), S, SE(South East is bottom right)
  • bg : The normal background color displayed behind the label and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • command : Function or method to be called when the scrollbar is moved.
  • width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.
  • elementborderwidth : The width of the borders around the arrowheads and slider. The default is elementborderwidth=-1, which means to use the value of the borderwidth option.
  • highlightbackground : The color of the focus highlight when the scrollbar does not have focus.
  • highlightcolor : The color of the focus highlight when the scrollbar has the focus.
  • highlightthickness : The thickness of the focus highlight. Default is 1. Set to 0 to suppress display of the focus highlight.
  • orient : Set orient=HORIZONTAL for a horizontal scrollbar, orient=VERTICAL for a vertical one.
  • jump : This option controls what happens when a user drags the slider. Normally (jump=0), every small drag of the slider causes the command callback to be called. If you set this option to 1, the callback isn’t called until the user releases the mouse button.
  • takefocus : Normally, you can tab the focus through a scrollbar widget. Set takefocus=0 if you don’t want this behavior.

Methods of Scrollbar or Slider Widget

  • get() : Returns two numbers (a, b) describing the current position of the slider. The a value gives the position of the left or top edge of the slider, for horizontal and vertical scrollbars respectively; the b value gives the position of the right or bottom edge.
  • set ( first, last ) : To connect a scrollbar to another widget w, set w’s xscrollcommand or yscrollcommand to the scrollbar’s set() method. The arguments have the same meaning as the values returned by the get() method.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :30th March 2020

Description : Program to show use of Scrollbar or Slider widget

"""
from tkinter import *
root = Tk()

slide = Scrollbar(root, orient=VERTICAL)
slide.pack(side=RIGHT, fill=Y)
mylist = Listbox(root, yscrollcommand = slide.set, font='Verdana',fg='black',bg='orange' ,width=30 )
 
for value in range(100):
    msg = " ITVoyagers prints number  " + str(value)
    mylist.insert(END, msg)
mylist.pack(side=LEFT, fill=BOTH)
slide.config( command = mylist.yview )

OUTPUT

Output of Scrollbar or Slider widget (itvoyagers.in)


You can also check following posts to know more about tkinter and GUI

Leave a Comment