The first 11 Chapter scroll bar
The function of the scroll bar is to make the content of the associated control scroll . Like text controls , For a long file , It is impossible to display it completely in the window , If you want to see the part that is not displayed , You need to use the scroll bar , Let the content that is not displayed appear in the display window . In general , Scroll bars are and list boxes 、 Canvas controls 、 Text control 、 Input controls, etc . The scroll bar can also be used alone . For example, when displaying the magnification or adjusting the value .
There are two kinds of scroll bars : Horizontal scroll bar and vertical scroll bar . The scroll bar consists of the following parts :
(1) slider
The slider is a raised rectangular pattern . You can drag it with the mouse , You can also use the button to adjust the position . You can also set the position by program . Slider movement , It also represents the change of display content or related values .
(2) Arrow button
The scroll bar has 2 Button :arrow1 and arrow2. The pattern of the button is generally arrow style . The horizontal scroll bar is located at the left and right ends , The vertical scroll bar is located at the upper and lower ends . The function is to adjust the display , Rolling content
(3) Rolling groove (trough )
The rolling groove is concave . According to the position of the slider , It is divided into trough1 and trough2. Click the scroll slot with the mouse , It can also realize scrolling display . Later chapters will introduce how to realize .
11.1 attribute
11.1.4 borderwidth(bd)
The border of the scroll bar . But in my windows It doesn't work in the environment .
11.1.5 command
Set the callback function , Used to handle rolling events . This parameter generally corresponds to the... Of the associated control xview perhaps yview Method . See Chapter 10 Text Control .
11.1.6 cursor
When the mouse passes the scroll bar , The shape of the mouse . For details, see 3.3.6 section
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Scrollbar(root,cursor='spider')
b1.pack(side = tk.RIGHT, fill = tk.Y)
root.mainloop()
11.1.7 elementborderwidth
Scroll bar slider and arrow border . But in my windows It doesn't work in the environment
11.1.8 highlightbackground,highlightcolor,highlightthickness
Scroll bar gets the background color when inputting focus 、 Foreground color and border . But it doesn't work
11.1.9 jump
jump=False When , Small change of slider , Will trigger the callback function .
jump=True When , Only when the mouse button is released , Will trigger the callback function .
11.1.10 orient
Set the direction of the scroll bar . There are two kinds of :tk.HORIZONTAL And tk.VERTICAL
11.1.11 relief
Set the appearance of the scroll bar . But it doesn't work .
11.1.12 repeatdelay
How long does the left mouse button press and hold in the scroll slot , The slider starts to move continuously . by default 300 millisecond . But it doesn't work
11.1.13 repeatinterval
Press and hold the mouse in the scroll slot , The moving frequency of the slider , How often the slider will move . The default time is 100 millisecond . It doesn't work .
11.1.14 takefocus
Set whether scrolling can be used Tab Key to get input focus .
11.1.15 troughcolor
Set the background color of the scroll slot . It doesn't work .
11.1.16 width
Set the width of the scroll bar . This is not how long the scroll bar is , But how wide .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Scrollbar(root,width=40)
b1.pack(side = tk.RIGHT, fill = tk.Y)
b2 = tk.Listbox(root, yscrollcommand = b1.set)
for line in range(100):
b2.insert(tk.END, "This is line number " + str(line))
b2.pack( side = tk.LEFT, fill = tk.BOTH )
b1.config( command = b2.yview )
root.mainloop()
result :
11.2 Method
11.2.1 activate
Activate the arrow buttons and sliders of the scroll bar . The reasonable input value is ’arrow1’,’arrow2’ and ’slider’. If no value is entered , Returns the currently active element . But it doesn't work .
11.2.2 delta(deltax, deltay)
Given a range of mouse movements deltax and deltay( In pixels ,deltax Indicates the horizontal movement ,deltay Indicates the amount of vertical movement ), Then the method returns a value of floating-point type ( Range -1.0 ~ 1.0), The return value indicates the position where the slider needs to move , To keep consistent with the movement of the mouse . This is usually used on mouse bindings , Used to determine how the slider moves when the user drags the mouse .
11.2.3 fraction(x,y)
According to the position of the mouse (x,y), Calculate the corresponding position of the slider .(x,y) All are in pixels . The range of the return value is (0.0,1.0).
11.2.4 get()
Get the position of the slider . The return value is (a,b).a Indicates the distance from the leftmost or uppermost side ( Look at the horizontal or vertical scroll bar ).b Indicates the distance from the rightmost or bottom . The value range is (0,1).
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Scrollbar(root,orient=tk.VERTICAL,width=40)
b1.pack(side = tk.RIGHT, fill = tk.Y)
b2 = tk.Listbox(root, yscrollcommand = b1.set)
for line in range(100):
b2.insert(tk.END, "This is line number " + str(line))
b2.pack( side = tk.LEFT, fill = tk.BOTH )
b1.config( command = b2.yview )
def get():
print(b1.get())
b3=tk.Button(root,text='Get',command=get)
b3.pack()
root.mainloop()
11.2.5 identify(x,y)
Judge whether the screen coordinate point is within the range of the scroll bar . If it is within the range of the scroll bar , Return the scroll bar element of this coordinate point :arrow1,arrow2,slider,trough1,trough2.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Scrollbar(root,orient=tk.VERTICAL,width=40)
b1.pack(side = tk.RIGHT, fill = tk.Y)
b2 = tk.Listbox(root, yscrollcommand = b1.set)
for line in range(100):
b2.insert(tk.END, "This is line number " + str(line))
b2.pack( side = tk.LEFT, fill = tk.BOTH )
b1.config( command = b2.yview )
def pos(event):
print(b1.identify(event.x,event.y))
b1.bind( "<Button-1>",pos)
root.mainloop()
11.2.6 set(lo,hi)
Associate the scroll bar with other controls . Set up xscrollcommand perhaps yscrollcommand The value of is .set. For detailed usage, see 11.2.5 The code in .