/Demo/tkinter/matt/canvas-with-scrollbars.py

http://unladen-swallow.googlecode.com/ · Python · 60 lines · 35 code · 15 blank · 10 comment · 0 complexity · 4e810fae7c7b4b4779e891197c47f1ec MD5 · raw file

  1. from Tkinter import *
  2. # This example program creates a scroling canvas, and demonstrates
  3. # how to tie scrollbars and canvses together. The mechanism
  4. # is analogus for listboxes and other widgets with
  5. # "xscroll" and "yscroll" configuration options.
  6. class Test(Frame):
  7. def printit(self):
  8. print "hi"
  9. def createWidgets(self):
  10. self.question = Label(self, text="Can Find The BLUE Square??????")
  11. self.question.pack()
  12. self.QUIT = Button(self, text='QUIT', background='red',
  13. height=3, command=self.quit)
  14. self.QUIT.pack(side=BOTTOM, fill=BOTH)
  15. spacer = Frame(self, height="0.25i")
  16. spacer.pack(side=BOTTOM)
  17. # notice that the scroll region (20" x 20") is larger than
  18. # displayed size of the widget (5" x 5")
  19. self.draw = Canvas(self, width="5i", height="5i",
  20. background="white",
  21. scrollregion=(0, 0, "20i", "20i"))
  22. self.draw.scrollX = Scrollbar(self, orient=HORIZONTAL)
  23. self.draw.scrollY = Scrollbar(self, orient=VERTICAL)
  24. # now tie the three together. This is standard boilerplate text
  25. self.draw['xscrollcommand'] = self.draw.scrollX.set
  26. self.draw['yscrollcommand'] = self.draw.scrollY.set
  27. self.draw.scrollX['command'] = self.draw.xview
  28. self.draw.scrollY['command'] = self.draw.yview
  29. # draw something. Note that the first square
  30. # is visible, but you need to scroll to see the second one.
  31. self.draw.create_rectangle(0, 0, "3.5i", "3.5i", fill="black")
  32. self.draw.create_rectangle("10i", "10i", "13.5i", "13.5i", fill="blue")
  33. # pack 'em up
  34. self.draw.scrollX.pack(side=BOTTOM, fill=X)
  35. self.draw.scrollY.pack(side=RIGHT, fill=Y)
  36. self.draw.pack(side=LEFT)
  37. def scrollCanvasX(self, *args):
  38. print "scrolling", args
  39. print self.draw.scrollX.get()
  40. def __init__(self, master=None):
  41. Frame.__init__(self, master)
  42. Pack.config(self)
  43. self.createWidgets()
  44. test = Test()
  45. test.mainloop()