📜  Python Tkinter滚动条

📅  最后修改于: 2020-10-27 01:59:21             🧑  作者: Mango

Python Tkinter滚动条

滚动条小部件用于向下滚动其他小部件的内容,例如列表框,文本和画布。但是,我们也可以为Entry小部件创建水平滚动条。

下面给出了使用滚动条小部件的语法。

句法

w = Scrollbar(top, options) 

下面列出了可能的选项。

SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 bg The background color of the widget.
3 bd The border width of the widget.
4 command It can be set to the procedure associated with the list which can be called each time when the scrollbar is moved.
5 cursor The mouse pointer is changed to the cursor type set to this option which can be an arrow, dot, etc.
6 elementborderwidth It represents the border width around the arrow heads and slider. The default value is -1.
7 Highlightbackground The focus highlighcolor when the widget doesn’t have the focus.
8 highlighcolor The focus highlighcolor when the widget has the focus.
9 highlightthickness It represents the thickness of the focus highlight.
10 jump It 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.
11 orient It can be set to HORIZONTAL or VERTICAL depending upon the orientation of the scrollbar.
12 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.
13 repeatinterval The default value of the repeat interval is 100.
14 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.
15 troughcolor It represents the color of the trough.
16 width It represents the width of the scrollbar.

方法

小部件提供以下方法。

SN Method Description
1 get() It returns the two numbers a and b which represents the current position of the scrollbar.
2 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.

from tkinter import *

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

mylist = Listbox(top, yscrollcommand = sb.set )

for line in range(30):
    mylist.insert(END, "Number " + str(line))

mylist.pack( side = LEFT )
sb.config( command = mylist.yview )

mainloop()

输出: