SCROLLBAR IN TKINTER
In this tutorial, we are going to learn about the scrollbar in Tkinter and how we can use it along with other widgets followed by a handy example.
What is Scrollbar in Tkinter?
The scrollbar widget in Tkinter is used along with other widgets like Listbox, Canvas, Text widget. You can use horizontal scrollbars with entry widgets if the information is spreading out. The syntax for scrollbar is:
scrollbar_tk = Scrollbar( window, features )
Scrollbar features and properties are:
1 | activebackground | Shows the background color of the scrollbar upon the mouse hover. |
2 | bg | Displays the background color of the scrollbar. |
3 | bd | It shows the width of the border. Same as borderwidth. |
4 | command | It is used to set the function call whenever there is some change. |
5 | cursor | Shows the type of cursor when the mouse is hovered over the scrollbar. The default is a pointed arrow cursor. |
6 | elementborderwidth | Specifies the border width of the arrow head in the slider. |
7 | Highlightbackground | It shows the highlight color of the background of the scrollbar when it is clicked. |
8 | highlightcolor | It shows the highlight color of the scrollbar when it is clicked. |
9 | highlightthickness | Specifies the thickness of the highlight in focus |
10 | jump | This feature controls the slider when the user drags the scrollbar. |
11 | orient | Specifies the vertical or horizontal orientation of the scrollbar. |
12 | repeatdelay | This option is used to define the duration when the button on the scrollbar slides through one range option to another. 300ms is the default duration. |
13 | repeatinterval | Sets the interval value |
14 | takefocus | By default you can tab the focus of the widget. You can set it to 0 if you want to skip this option. |
15 | troughcolor | Specifies the color of the trough |
16 | width | Shows the width of the scrollbar. |
Methods of scroll are:
1 | get() | Returns the current position of the scrollbar |
2 | set(hi, lo) | Moves the slider to a new position by specifying hi and lo position. |
Creating Scrollbar Widget
You can create simple a scrollbar as follows:
from tkinter import * screen = Tk() screen.geometry('400x400') scrollbar_tk = Scrollbar(screen) scrollbar_tk.pack(side=RIGHT, fill=Y) listbox_tk = Listbox(screen, yscrollcommand=scrollbar_tk.set) for i in range(100): listbox_tk.insert(END, str(i)) listbox_tk.pack(side=LEFT, fill=BOTH) scrollbar_tk.config(command=listbox_tk.yview) mainloop()
Output will be:
References have been used from here