程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Tkinter -- Chapter 11 scroll bar

編輯:Python

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

attribute describe activebackground When the mouse is up , The background color of the scroll bar activerelief When the mouse passes , Decorative effect of scroll bar backgroud
bg The background color of the button borderwidth
bd Size of border , The default is 2 Pixel command Associated function , When scrolling is clicked , Execute this function cursor The shape of the cursor is set , Such as arrow, circle, cross, plus etc. elementborderwidth The width of the constituent elements highlightcolor Highlight color of focus highlightbackground Highlighted background color of focus highlightthickness Focus highlight border width jump The slider jumps to the specified position .Orient Set the direction of the scroll bar . It can be VERTICAL Or is it HORIZONALrelief Border style , Setting controls 3D effect , Optional :FLAT、SUNKEN、RAISED、GROOVE、RIDGE. The default is FLAT.repeatdelay The default value is 300repeatinterval The default value is 100takefocus Indicates that it can be used Tab Key to move the focus to the control . The default is an empty string .troughcolor The background color of the scroll slot width Define the width of the scroll bar . The default value is 1511.1.1 activebackground When the mouse passes the scroll bar , Color of slider and arrow buttons . But in my windows It doesn't work in the environment .11.1.2 activerelief When the mouse passes the scroll bar , Decorative effect of scroll bar . But in my windows It doesn't work in the environment .11.1.3 backgroud(bg) The background color of the scroll bar . But in my windows It doesn't work in the environment .

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

Method describe activate(element) Activate the scroll bar element .
element Include :”arrow1”,”arrow2”,”slider”. If there is no input element, Then return the currently active element . If there is no active element , Returns an empty string .config(**options) Configure the properties of the scroll bar . For the specific attribute description, see the scroll bar attribute description .delta(deltax, deltay) This function deals with mouse dragging . According to the position of the mouse on the screen , Calculate the offset that the scroll bar slider needs to move , So as to achieve the purpose of dragging .
deltax: The horizontal scroll block adds value .
deltay: The vertical scroll block adds value
Return value : The offset value to be added to the scroll bar .fraction(x, y)X: Horizontal position of the mouse
Y: Vertical position of the mouse
Return value : The sliding block offset returned according to the position of the mouse .(0.0-1.0).get() Get the position of the current slider .identify(x, y) Confirm the coordinate point of the input screen , Whether it is within the scope of the scroll bar control .
(x,y): Screen coordinate point
Return value : character string
“arrow1”: Top left corner or top
“arrow2”: On the far right, the latter is at the bottom
“slider”: On the slider .
“trough1”: Above or to the left of the slider .
“trough2”: Below or to the right of the slider
None: Not within the scroll bar control .set(lo,hi) Move the slider to a new position .
Lo: Relative to the top ( Leftmost left ) The migration
Hi: Relative to the bottom ( The most right ) The migration .

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved