The Scrollbar component is used to scroll the visible range of some components and can be categorized into vertical and horizontal scrollbars depending on the direction.The Scrollbar component is often used to implement the scrolling of text, canvas and listboxes.
When to use the Scrollbar component?
Scrollbar components are often used almost exclusively with Text, Canvas, and Listbox components, and horizontal scrollbars can also be used withEntry Component fit.
usage
In the following example we demonstrate how to use vertical scrollbars. In order to install vertical scrollbars on a component, you need to do two things:
1. Set the yscrollbarcommand option of this component to the set() method of the Scrollbar component;
2. Set the command option of the Scrollbar component to the yview() method of the component.
import tkinter as tk root = () sb = (root) (side="right", fill="y") lb = (root, yscrollcommand=) for i in range(1000): ("end", str(i)) (side="left", fill="both") (command=) ()
Analysis: The Listbox component notifies the Scrollbar component when the viewable range of the Listbox component changes by calling the set() method. And when the user manipulates the scrollbar, the yview() method of the Listbox component will be called automatically.
Adding a horizontal scrollbar is the same as above, just change yscrollcommand to xscrollcommand and yview to xview.
parameters
Scrollbar(master=None, **options) (class)
master -- parent component
**options -- component options, the table below details the meaning and usage of each option:
options (as in computer software settings) | hidden meaning |
activebackground | 1. Specify the background color of the sliders and arrows when the mouse floats over them. 2. Default values are specified by the system |
activerelief | 1. Specify the style of the slider when the mouse floats over it. 2. The default value is "raised" 3. "flat", "sunken", "groove", "ridge" can be selected. |
background | 1. Specify background color 2. Default values are specified by the system |
bg | Same as background. |
borderwidth | 1. Specify border width 2. The default value is 0 |
bd | Same as borderwidth |
command | 1. Functions that are called back when the scrollbar is updated 2. It is common to specify the xview() or yview() method of the corresponding component. |
cursor | 1. Specify the mouse style when the mouse floats over the top 2. Default values are specified by the system |
elementborderwidth | 1. Specify the border width of scrollbars and arrows 2. The default value is -1 (the value that indicates the use of the borderwidth option) |
highlightbackground | 1. Specify the color of the highlighted border when the scrollbar does not have focus 2. Default values are specified by the system |
highlightcolor | 1. Specify the color of the highlighted border when the scrollbar gets the focus. 2. Default values are specified by the system |
highlightthickness | 1. Specify the width of the highlighting border 2. Default value is 0 (without highlight border) |
jump | 1. Specify the behavior when the user drags the scrollbar 2. The default value is False, and any movement of the scrollbar will instantly call the callback function specified by the command option. 3. Set to True to be called when the user releases the mouse. |
orient | 1. Specify whether to draw "horizontal" or "vertical" scrollbars. 2. The default value is VERTICAL |
relief | 1. Specify the border style 2. The default value is "sunken" 3. The options are "flat", "raised", "groove", "ridge". |
repeatdelay | 1. This option specifies the response time for a left mouse button click on a scroll bar notch. 2. Default value is 300 (milliseconds) |
repeatinterval | 1. This option specifies the response interval when the left mouse button is pressed firmly against the scrollbar notch. 2. Default value is 100 (milliseconds) |
takefocus | 1. Specify that the Tab key can be used to move focus to this Scrollbar component. 2. is on by default and can be set to False to avoid focusing on this component |
troughcolor | 1. Specify the color of the notch 2. Default values are specified by the system |
width | 1. Specify the width of the scrollbar 2. The default value is 16 pixels |
methodologies
activate(element)
-- Displays the background color and style of the element specified by the element parameter.
The -- element parameter can be set to: "arrow1", "arrow2" or "slider".
delta(deltax, deltay)
-- Given a range of mouse movements deltax and deltay (in pixels, deltax is the amount of horizontal movement, deltay is the amount of vertical movement), the method returns a floating-point value (range -1.0 ~ 1.0).
-- This is typically used on mouse bindings to determine how the slider moves when the user drags the mouse.
fraction(x, y)
-- Given a pixel coordinate (x, y), this method returns the scrollbar position closest to the given coordinate (range 0.0 ~ 1.0)
get()
-- Returns the position of the current slider (a, b)
-- a value indicates the top or left position of the current slider, b value indicates the bottom or right position of the current slider (range 0.0 ~ 1.0)
identify(x, y)
-- Returns a string representing the scrollbar widget at the specified position (if any).
-- The return value can be: "arrow1" (arrow1), "arrow2" (arrow2), "slider" or "" (nothing).
set(*args)
-- Set the position of the current scrollbar
-- If set, two parameters (first, last) are required, first indicates the top or left position of the current slider, last indicates the bottom or right position of the current slider (range 0.0 ~ 1.0)
to this article on the specific use of the Tkinter component Scrollbar article is introduced to this, more related Tkinter Scrollbar content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!