Advertisement
here2share

# Tk_scroll.py

Sep 5th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. # Tk_scroll.py
  2.  
  3. from Tkinter import *
  4.  
  5. parent=Tk() # parent object
  6. canvas = Canvas(parent, height=200) # a canvas in the parent object
  7. frame = Frame(canvas) # a frame in the canvas
  8. # a scrollbar in the parent
  9. scrollbar = Scrollbar(parent, orient="vertical", command=canvas.yview)
  10. # connect the canvas to the scrollbar
  11. canvas.configure(yscrollcommand=scrollbar.set)
  12. scrollbar.pack(side="right", fill="y") # comment out this line to hide the scrollbar
  13. canvas.pack(side="left", fill="both", expand=True) # pack the canvas
  14. # make the frame a window in the canvas
  15. canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")
  16. # bind the frame to the scrollbar
  17. frame.bind("<Configure>", lambda x: canvas.configure(scrollregion=canvas.bbox("all")))
  18. parent.bind("<Down>", lambda x: canvas.yview_scroll(3, 'units')) # bind "Down" to scroll down
  19. parent.bind("<Up>", lambda x: canvas.yview_scroll(-3, 'units')) # bind "Up" to scroll up
  20. # bind the mousewheel to scroll up/down
  21. parent.bind("<MouseWheel>", lambda x: canvas.yview_scroll(int(-1*(x.delta/40)), "units"))
  22. labels = [Label(frame, text=str(i)) for i in range(30)] # make some Labels
  23. for l in labels: l.pack() # pack them
  24.  
  25. parent.mainloop() # run program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement