Faqts : Business : Programming : Shopping For You : Python : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 12 people (83%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

In Tkinter, can you connect a scrollbar to 2 objects?

Dec 18th, 2009 18:53
new acct, osama said, Scott Mandarich, osososo didididi, unknown unknown, Matthew Dixon Cowles


Problem:

In Tkinter, can you connect a scrollbar to 2 objects (In my case I need
it to be on 2 canvases, or canvii, if you prefer).  The
canvases(canvaii) are next to each other horizontally, and I want them
to scroll down like one canvas.  I would just combine them, but I need
two so that the horizontal scroll will work correctly.

Solution:

Yes, you can. All you need to do is to set the scrollbar's command to
your own function that passes the arguments it's called with to the
xview or yview methods of both canvases. I'll append an example.

Isn't "caravanserai" the plural of "canvas" <wink>.


from Tkinter import *

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

  def createWidgets(self):
    self.c1=Canvas(self.tkRoot,bg="blue",width="2i",height="2i", \
      scrollregion=(0, 0, "4i", "4i"))
    self.c1.pack(side=LEFT)
    self.c2=Canvas(self.tkRoot,bg="green",width="2i",height="2i", \
     scrollregion=(0, 0, "4i", "4i"))
    self.c2.pack(side=LEFT)
    self.sb=Scrollbar(orient="vertical")
    self.sb.pack(side=LEFT,fill=Y)
    self.sb['command']=self.scrollTwo
    self.c2['yscrollcommand']=self.sb.set

    self.c1.create_rectangle("0.5i", "0.5i", "1i", "1i", fill="black")
    self.c2.create_rectangle("0.5i", "0.5i", "1i", "1i", fill="yellow")

    return None

  def scrollTwo(self,*args):
    print args
    self.c1.yview(*args)
    self.c2.yview(*args)
    return None

def main():
  tkRoot=Tk()
  mainWin(tkRoot)
  tkRoot.mainloop()
  

if __name__=='__main__':
  main()