Python GUI programming (eight) Scrollbar (scroll bar)

Source: Internet
Author: User
Tags pack

Scrollbar (scroll bar) that can be used alone, but most of it is used in conjunction with other controls (LISTBOX,TEXT,CANVA, etc.)

The initial position setting of the slider: Set () method

Set (self, *args): "" "
    set the fractional values of the slider position (upper and
    lower ends as value between 0 and 1).
    "" " ' Set ') + args)
The value set is between 0---1

From tkinter Import *
root=tk ()
root.geometry (' 200x100 ')

S1=scrollbar (root,orient=horizontal)
S1.set (0.6,0)
s1.pack ()
Root.mainloop ()

Listbox and ScrollBar Binding

From Tkinter import *
root = Tk ()
lb = Listbox (root)
sl = Scrollbar (root)
sl.pack (side = Right,fill = Y) 
   #side指定Scrollbar为居右 Fill fills the entire remaining area.
#下面的这句是关键: The callback function for the Yscrollbar of the specified listbox is scrollbar set
lb[' yscrollcommand '] = Sl.set for
i in range (100):
    Lb.insert (End,str (i))
#side指定Listbox为居左
lb.pack (side = left)
# The following sentence is the key: The callback function that specifies the command of the scrollbar is the Listbar yview
sl[' command ' = Lb.yview root.mainloop
()

ListBox properties and Methods

Xscrollcommand Properties:

If you want the user to scroll horizontally through the list box, you can link your listbox WIDG et to a horizontal scroll bar. Yscrollcommand Properties:

If you want the user to scroll vertically through the list box, you can link your listbox WIDG et to the vertical scroll bar.

Yview (): Scrolls the list box vertically, setting the command options for the associated vertical scroll bar to this method.


From tkinter Import *
root=tk ()

lb=listbox (root)
# horizontal scrolling
S1=scrollbar (root,orient=horizontal)

S1.pack (side=bottom,fill=x)
lb[' Xscrollcommand ']=s1.get () for
I in range (MB):
    Lb.insert (End,i)
Lb.pack (side=top)
s1[' command ']=lb.yview
root.mainloop ()
There 's something wrong with the program.

When the listbox changes, ScrollBar calls set to change the position of the slder, and when ScrollBar changes the slider position, the listbox calls Yview to display the new list item. To demonstrate the two relationships, unbind the Yscrollcommad with the scrollbar set.

From Tkinter import *
root = Tk ()
lb = Listbox (root)
sl = Scrollbar (root)
sl.pack (side = Right,fill = Y)
#解除Listbox的yscrollcommand与Scrollbar的set绑定
#lb [' yscrollcommand '] = Sl.set for
i in range (100):
    Lb.insert (End,str (i))
#使用索引为50的元素可见
lb.see (m)
lb.pack (side = left)
sl[' command '] = Lb.yview
Root.mainloop ()
Run results, the listbox shows 50 items, that is, the view of the ListBox is 50, but ScrollBar's slider is still at 0. That is to say Scroolbar did not receive the set command. That is, if you unbind this binding, ScrollBar will no longer respond to messages that are changed by the listbox view. However, you can still use the ScrollBar slider to move the view of the listbox.

The relationship between the command of ScrollBar and the Yview of the listbox

From Tkinter import *
root = Tk ()
lb = Listbox (root)
sl = Scrollbar (root)
sl.pack (side = Right,fill = Y) 
   #下面的这句是关键: The callback function for the Yscrollbar of the specified listbox is scrollbar set
lb[' yscrollcommand '] = Sl.set for
i in range (100):
    Lb.insert (End,str (i*100))
#使用索引为50的元素可见
lb.see
lb.pack (side = left)
# ScrollBar the relationship between the command and the Yview of the listbox
#sl [' command '] = Lb.yview
root.mainloop ()
Run the program, ScrollBar's slider has been to 50-bit, that is to say scrollbar response to the ListBox view changes in the message, called its own set function.
To do: Drag Slder or click the Up/down button, the view of the ListBox does not respond, that is, the listbox does not respond to scrollbar messages.


Related Article

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.