Scrolling Multiple Listboxes

Matthew Dixon Cowles matt at mondoinfo.com
Fri Jun 1 21:23:19 EDT 2001


On 1 Jun 2001 11:19:59 -0700, kmdr <kmdr2001 at hotmail.com> wrote:

>Does anyone know how to bind multiple listboxes to one scrollbar?
>ie.  how do I scroll multiple listboxes with one scrollbar?...I've
>tried apply(object, args) methods but have not been succesful.

Kapil,
I'm sure that you're close to the solution. Interestingly, this is
where Tkinter's strange scrollbar configuration comes in handy. What
you do is make the scrollbar's command a routine that calls both
listboxes' yview methods. I'll append an example:

Regards,
Matt

#!/usr/local/bin/python

from Tkinter import *

class mainWin:
  def __init__(self,root):
    self.root=root
    self.createWidgets()
    return None

  def createWidgets(self):
    self.l1=Listbox(self.root)
    self.l1.pack(side=LEFT)
    self.l2=Listbox(self.root)
    self.l2.pack(side=LEFT)
    for count in range(42):
      self.l1.insert(END,"l1 "+str(count))
      self.l2.insert(END,"l2 "+str(count))
    
    s=Scrollbar(self.root,orient=VERTICAL,command=self.scrollBoth)
    s.pack(side=LEFT,fill=Y)
    self.l1.configure(yscrollcommand=s.set)
    return None

  def scrollBoth(self,*args):
    apply(self.l1.yview,args)
    apply(self.l2.yview,args)
    return None

def main():
  root=Tk()
  mainWin(root)
  root.mainloop()
  return None

if __name__=="__main__":
  main()



More information about the Python-list mailing list