Python --- (eight) Tkinter widget: Scrollbar

Previous: Python - (seven) Tkinter widgets: Listbox


                                          The Tkinter Scrollbar Widget

## Introduction

ScrollBar (a scroll bar) rolling assembly for some components of the visible range, can be divided according to the direction of the vertical scroll bar and a horizontal scroll bar. Scrollbar component is often used to implement scrolling text, canvas and a list box.

When to use ## Scrollbar component?

Scrollbar component is usually almost the Text component, Canvas components and Listbox used with components, the horizontal scroll bar can now Entry mating components.

We examples below demonstrate how to use the vertical scroll bar. In order to install the vertical scroll bar on a component, you need to do two things:

##usage

1. Set yscrollbarcommand options of this component is set Scrollbar component () method;

2. Set the Scrollbar component yview command options for the components () method.

from tkinter import *

root = Tk()

sb = Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)

lb = Listbox(root,yscrollcommand=sb.set)

for i in range(1000):
    lb.insert(END,i)

lb.pack(side = LEFT,fill=BOTH)

sb.config(command=lb.yview)

mainloop()

Here Insert Picture Description
       (Analysis: First, increase the Listbox, lb = Listbox (root, yscrollcommand = sb.set) to give Listbox this component plus a scroll bar, you must set yscrollcommand or xscrollcommand option (a vertical scroll is a horizontal scrolling) this two values scrollbar object instance is set to sb method
       then circulating it to add content Listbox component, after addition Pick (), has just completed the first condition, keep the scroll bar interconnection, then there is also need to set the scroll bar a command option, sb.config (command = lb.yview), then you can directly call the object config method to set his value, should be noted that, because at this time, lb was produced before pick out, if you set the pick () will be a problem before then is to set sb that is rolling component command options for yview method Listbox components like,
       let's analyze what his process: it is in fact a process of interconnection , when the user operates the scroll bar to scroll when the scroll bar in response to the scroll operation by the user is first, followed by yview () method (Li stbox of a default method), in fact, is how to set the vertical scroll display corresponding changes in the content, the content will automatically refresh, and then, if there is a Listbox scrolling (ie direct manipulation with the mouse wheel), it will call sb.set () method, while modifying the position of the scroll bar.)

##parameter

Scrollbar(master=None, **options) (class)

master - parent component

** options - component options, the table below details the specific meaning and usage of each option:

Options meaning
activebackground 1. When the mouse is over the specified time drifting slider and the background color of the arrow
2. The default value is specified by the system
activerelief 1. When the designated style mouse over drifting when the slider
2. The default value is "raised"
3. can select "flat", "sunken", "groove", "ridge"
background 1. Specify the background color
2. The default value is specified by the system
bg Like background
borderwidth 1. Specify the width of the frame
2. The default value is 0
bd Like borderwidth
command 1. When the callback function updates the scroll bar
2 is designated generally corresponding components of the XView () or yview () method
cursor 1. Specify drifting when the mouse over the mouse style
2. The default value is specified by the system
elementborderwidth 1. Specify the scroll bar and arrows border width
2. The default value is -1 (a value indicating the use borderwidth option)
highlightbackground 1. When the specified color is not obtained when the scroll bar is highlighted border of the focus
2. The default value is specified by the system
highlightcolor 1. Specify obtained when the scroll bars when focus color highlighting border
2. The default value is specified by the system
highlightthickness 1. Specify the width of the highlight border
2. The default value is 0 (without border highlighted)
jump 1. 指定当用户拖拽滚动条时的行为
2. 默认值是 False,滚动条的任何一丝变动都会即刻调用 command 选项指定的回调函数
3. 设置为 True 则当用户松开鼠标才调用
orient 1. 指定绘制 "horizontal"(垂直滚动条)还是 "vertical"(水平滚动条)
2. 默认值是 VERTICAL
relief 1. 指定边框样式
2. 默认值是 "sunken"
3. 可以选择 "flat","raised","groove","ridge"
repeatdelay 1. 该选项指定鼠标左键点击滚动条凹槽的响应时间
2. 默认值是 300(毫秒)
repeatinterval 1. 该选项指定鼠标左键紧按滚动条凹槽时的响应间隔
2. 默认值是 100(毫秒)
takefocus 1. 指定使用 Tab 键可以将焦点移到该 Scrollbar 组件上
2. 默认是开启的,可以将该选项设置为 False 避免焦点在此组件上
troughcolor 1. 指定凹槽的颜色
2. 默认值由系统指定
width 1. 指定滚动条的宽度
2. 默认值是 16 像素

##方法

activate(element)
— 显示 element 参数指定的元素的背景颜色和样式
—element 参数可以设置为:“arrow1”(箭头1),“arrow2”(箭头2)或 “slider”(滑块)

delta(deltax, deltay)
— 给定一个鼠标移动的范围 deltax 和 deltay(像素为单位,deltax 表示水平移动量,deltay 表示垂直移动量),然后该方法返回一个浮点类型的值(范围 -1.0 ~ 1.0)
— 这通常在鼠标绑定上使用,用于确定当用户拖拽鼠标时滑块的如何移动

fraction(x, y)
— 给定一个像素坐标 (x, y),该方法返回最接近给定坐标的滚动条位置(范围 0.0 ~ 1.0)

get()
— 返回当前滑块的位置 (a, b)
— a 值表示当前滑块的顶端或左端的位置,b 值表示当前滑块的底端或右端的位置(范围 0.0 ~ 1.0)

identify(x, y)
— 返回一个字符串表示指定位置下(如果有的话)的滚动条部件
— 返回值可以是:“arrow1”(箭头1),“arrow2”(箭头2)、“slider”(滑块)或 “”(啥都没有)

set(*args)
— 设置当前滚动条的位置
— 如果设置则需要两个参数 (first, last),first 表示当前滑块的顶端或左端的位置,last 表示当前滑块的底端或右端的位置(范围 0.0 ~ 1.0)


下一篇:Python ----(九)Tkinter窗口组件:Scale

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104165163