Reputation: 601
Here is my Tkinter code. I intend to show the both horizontal and vertical scrollbar.
from Tkinter import *
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT,fill = Y)
scrollbar_x = Scrollbar(root)
scrollbar_x.pack(side = BOTTOM,fill = X)
mylist = Listbox(root,xscrollcommand = scrollbar_x.set,yscrollcommand = scrollbar.set)
"""
To connect a scrollbar to another widget w, set w's xscrollcommand or yscrollcommand to the scrollbar's set() method. The arguments have the same meaning as the values returned by the get() method.
"""
for line in range(100):
mylist.insert(END,("this is line number"+str(line))*100)
mylist.pack(side = LEFT,fill=BOTH)
scrollbar.config(command = mylist.yview)
scrollbar_x.config(command = mylist.xview)
print root.pack_slaves()
mainloop()
And it doesn't match up to my expectation in my computer, which is Mac Sierra.
Furthermore, what's scrollbar.config(command = mylist.yview)
means?
config()
More details about this function will be helpful~ thanks
Upvotes: 0
Views: 61
Reputation: 386362
Tkinter program: doesn't show me the bottom scrollbar
I can't duplicate that when I run your code. When I run your code I see a giant scrollbar at the bottom of the screen.
I presume you want the "x" scrollbar to be horizontal rather than vertical. If that is true, you must tell tkinter that by using the orient
option:
scrollbar_x = Scrollbar(root, orient="horizontal")
Furthermore, what's scrollbar.config(command = mylist.yview) means?
Scrollbars require two-way communication. The listbox needs to be configured to know which scrollbar to update when it has been scrolled, and the scrollbar needs to be configured such that it knows which widget to modify when it is moved.
When you do scrollbar.config(command=mylist.yview)
you are telling the scrollbar to call the function mylist.yview
whenever the user tries to adjust the scrollbar. The .yview
method is a documented method on the Listbox
widget (as well as a few others). If you do not set the command
attribute of a scrollbar, interacting with the scrollbar will have no effect.
Upvotes: 1