Advertisement
here2share

# Tk_hide_scrollbar.py

Oct 5th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. # Tk_hide_scrollbar.py
  2. # by window stretched
  3.  
  4. from Tkinter import *
  5. from PIL import Image, ImageTk
  6.  
  7. def on_vertical(event):
  8.     canvas.yview_scroll(-event.delta/100, 'units')
  9.  
  10. def on_horizontal(event):
  11.     canvas.xview_scroll(-event.delta/100, 'units')
  12.  
  13. def autoscroll(sbar, first, last):
  14.     """hide and show scrollbar as desired"""
  15.     first, last = float(first), float(last)
  16.     if first <= 0 and last >= 1:
  17.         sbar.grid_remove()
  18.     else:
  19.         sbar.grid()
  20.     sbar.set(first, last)
  21.  
  22. def getImage():
  23.     global imageWhere
  24.     theImage = ImageTk.PhotoImage(Image.open(imageWhere))
  25.     return theImage
  26.  
  27. root = Tk()
  28. hbar = Scrollbar(root, orient=HORIZONTAL)
  29. vbar = Scrollbar(root, orient=VERTICAL)
  30. canvas = Canvas(root, scrollregion=(0, 0, 500, 500), yscrollcommand=lambda f, l: autoscroll(vbar, f, l), xscrollcommand=lambda f, l:autoscroll(hbar, f, l))
  31. hbar['command'] = canvas.xview
  32. vbar['command'] = canvas.yview
  33. imageWhere = r'C:\tests4python\imgs\test320x320.png'
  34. theImage = getImage()
  35. canvas.create_image(0,0,image=theImage, anchor='nw')
  36. canvas.grid(column=0, row=0, sticky=(N,W,E,S))
  37.  
  38. canvas.bind_all('<MouseWheel>', on_vertical)
  39. canvas.bind_all('<Shift-MouseWheel>', on_horizontal)
  40.  
  41. hbar.grid(column=0, row=1, sticky=(W,E))
  42. vbar.grid(column=1, row=0, sticky=(N,S))
  43. root.grid_columnconfigure(0, weight=1)
  44. root.grid_rowconfigure(0, weight=1)
  45. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement