Scrollbar for Tkinter tutorials

Source: Internet
Author: User
''' Scrollbar for Tkinter tutorials '''
# Scrollbar (scroll bar), which can be used independently, but most of them are used in combination with other controls (Listbox, Text, Canva, etc.)
'''1 create a Scrollbar '''
From Tkinter import *
Root = Tk ()
Scrollbar (root). pack ()
Root. mainloop ()
# A Scrollbar is displayed, but nothing can be done. slider cannot be dragged.
From Tkinter import *
Root = Tk ()
Sl = Scrollbar (root)
Sl. set (0.5, 0)
Sl. pack ()
Root. mainloop ()
'''2. set the slider location using the set method '''
# Use a horizontal scroll bar and set the value to (0.5, 1) Through set, that is, slider occupies half of the Srollbar.
From Tkinter import *
Root = Tk ()
Sl = Scrollbar (root, orient = HORIZONTAL)
Sl. set (0.5, 1)
Sl. pack ()
Root. mainloop ()
'''3. Use the callback function (not recommended )'''
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
Def scrollCall (moveto, pos ):
# How to obtain two parameters: Use the following printed information to view the two parameters passed by the interpreter to the scrollCall function.
# Moveto. We can see from the reference manual that it is the function called when the slider is dragged. The other parameter is the current position of the slider.
# You can use the set function to set the position of the slider. Therefore, you can use this pos function to control the position of the slider.
# Print moveto, pos
Sl. set (pos, 0)
Print sl. get ()
Sl = Scrollbar (root, orient = HORIZONTAL, command = scrollCall)
Sl. pack ()
Root. mainloop ()
# There is another serious problem. You can only drag it. For the response of two buttons and pagedwon/pageup
# An exception occurs. This example is only used to show that the command attribute is available. If you like to process all the messages, can you change scrollCall to a variable parameter function?
# Process different inputs separately.

'''4. It is rare to use it independently. Most applications are bound to other controls. Below is an example of binding a Listbox to a Scrollbar '''
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
Lb = Listbox (root)
Sl = Scrollbar (root)
Sl. pack (side = RIGHT, fill = Y)
# Side specifies that the Scrollbar is left to the right; fill specifies that the entire remaining area is filled up. By the time WM is running, these attributes are described in detail.
# The following sentence is the key: Specify the yscrollbar callback function of Listbox as the set of Scrollbar.
Lb ['yscrollcommand'] = sl. set
For I in range (100 ):
Lb. insert (END, str (I ))
# Specify the Listbox to the left of the side
Lb. pack (side = LEFT)
# The following sentence is the key: specify that the callback function of the Scrollbar command is the yview of Listbar.
Sl ['command'] = lb. yview
Root. mainloop ()
'''5. in this way, the relationship between the two is understood: When the Listbox is changed, Scrollbar calls set to change the position of slder; when the Scrollbar changes the position of slider, Listbox calls yview to display the new list item, to demonstrate the two relationships, unbind yscrollcommad from the scrollbar set to see what effect '''
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
Lb = Listbox (root)
Sl = Scrollbar (root)
Sl. pack (side = RIGHT, fill = Y)
# Unbind the yscrollcommand of Listbox from the set of Scrollbar
# Lb ['yscrollcommand'] = sl. set
For I in range (100 ):
Lb. insert (END, str (I ))
# Visible with 50 indexed Elements
Lb. see (50)
Lb. pack (side = LEFT)
Sl ['command'] = lb. yview
Root. mainloop ()
# In the running result, The Listbox displays 50 items, that is, the Listbox view has reached 50, but the slider Of The Scrollbar is still located at 0. That is to say, Scroolbar does not receive the set
. That is to say, the Scrollbar will no longer respond to the message changed in The Listbox view. However, you can still use the slider Of The Scrollbar to move the Listbox view.

'''6. Test the relationship between Scrollbar command and Listbox yview again. The test code is as follows :'''
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
Lb = Listbox (root)
Sl = Scrollbar (root)
Sl. pack (side = RIGHT, fill = Y)
# The following sentence is the key: Specify the yscrollbar callback function of Listbox as the set of Scrollbar.
Lb ['yscrollcommand'] = sl. set
For I in range (100 ):
Lb. insert (END, str (I * 100 ))
# Visible with 50 indexed Elements
Lb. see (50)
Lb. pack (side = LEFT)
# Relationship between Scrollbar command and Listbox yview
# Sl ['command'] = lb. yview
Root. mainloop ()
# Run the program. The slider Of The Scrollbar has reached the 50 position. That is to say, the Scrollbar responds to the message changed from The Listbox view and calls its own set function.
# Operation: Drag slder or click the up/down button. The Listbox view does not respond, that is, Listbox does not respond to the Scrollbar message.

# Author: jcodeer
# Blog: jcodeer.cublog.cn
# Email: jcodeer@126.com

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.