1K
In this article we show you how to add scrollbars to tour Tkinter gui.
You can create a horizontal or vertical scrollbars
Syntax
w = Scrollbar(top, options)
This is a list of possible options.
Option | Description |
---|---|
activebackground | The background color of the widget when it has the focus. |
bg | The background color of the widget. |
bd | The border width of the widget. |
command | It can be set to the procedure associated with the list which can be called each time when the scrollbar is moved. |
cursor | The mouse pointer is changed to the cursor type set to this option which can be an arrow, dot, etc. |
elementborderwidth | It represents the border width around the arrow heads and slider. The default value is -1. |
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 | This represents the thickness of the focus highlight. |
jump | This is used to control the behavior of the scroll jump. If it set to 1, then the callback is called when the user releases the mouse button. |
orient | This can be set to HORIZONTAL or VERTICAL depending upon the orientation of the scrollbar. |
repeatdelay | This option tells the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. The default is 300 ms. |
repeatinterval | The default value of the repeat interval is 100. |
takefocus | We can tab the focus through this widget by default. We can set this option to 0 if we don't want this behavior. |
troughcolor | This represents the color of the trough. |
width | This represents the width of the scrollbar. |
Methods
The widget provides the following methods.
Method | Description |
---|---|
get() | It returns the two numbers a and b which represents the current position of the scrollbar. |
set(first, last) | It is used to connect the scrollbar to the other widget w. The yscrollcommand or xscrollcommand of the other widget to this method. |
Examples
Here is a standard description
from tkinter import * root = Tk() root.geometry("150x200") w = Label(root, text ='maxpython', font = "50") w.pack() scroll_bar = Scrollbar(root) scroll_bar.pack( side = RIGHT, fill = Y ) mylist = Listbox(root, yscrollcommand = scroll_bar.set ) for line in range(1, 26): mylist.insert(END, "Entry number " + str(line)) mylist.pack( side = LEFT, fill = BOTH ) scroll_bar.config( command = mylist.yview ) root.mainloop()
This will display something like this