简体   繁体   中英

python GUI tkinter scrollbar not working for canvas

I am learning python GUI with Tkinter. i just created two listbox and populated those in a canvas so that i can configure canvas with scrollbar and those two listbox scroll togather when i scroll canvas. but something is not working.

Here is the structure:

canvas = Canvas(master)
scrollBar = Scrollbar(master)
movieListBox = Listbox(canvas) 
statusListBox = Listbox(canvas)
canvas.config(yscrollcommand = scrollBar.set)
movieListBox.config(width = 55)
statusListBox.config(width = 8)
movieListBox.pack(fill = "y", side = "left")
statusListBox.pack(fill = "y", side = "right")
canvas.pack(side = "left", fill = "y", expand = "true")
scrollBar.config(command = canvas.yview)
scrollBar.pack(fill = "y", side = "left")

for i in range(500):
    movieListBox.insert(i, "movie name")
    statusListBox.insert(i, "downloading")

master.mainloop()

在此处输入图片说明

I'm pretty new to tkinter and python myself, but I did find something that works. This is from another question, (I didn't write the code) which was about having two listboxes of different lengths linked to the same scrollbar. If you copy their code (below) you'll see that it works fine for listboxes of equal length. Your question is pretty old, but I hope it helps someone. Cheers.

Where I found this:

Scrolling two listboxes of different lengths with one scrollbar

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class App(object):
    def __init__(self,master):
        scrollbar = tk.Scrollbar(master, orient='vertical')
        self.lb1 = tk.Listbox(master, yscrollcommand=scrollbar.set)
        self.lb2 = tk.Listbox(master, yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.yview)
        scrollbar.pack(side='right', fill='y')
        self.lb1.pack(side='left', fill='both', expand=True)
        self.lb2.pack(side='left', fill='both', expand=True)

    def yview(self, *args):
        """connect the yview action together"""
        self.lb1.yview(*args)
        self.lb2.yview(*args)


root = tk.Tk()
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("320x180+130+180")
root.title("connect 2 listboxes to one scrollbar")

app = App(root)

# load the list boxes for the test
for n in range(64+26, 64, -1): #listbox 1
    app.lb1.insert(0, chr(n)+'ell')

for n in range(70+30, 64, -1):    
    app.lb2.insert(0, chr(n)+'ell') #listbox 2

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM