Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_hide_scrollbar.py
- # by window stretched
- from Tkinter import *
- from PIL import Image, ImageTk
- def on_vertical(event):
- canvas.yview_scroll(-event.delta/100, 'units')
- def on_horizontal(event):
- canvas.xview_scroll(-event.delta/100, 'units')
- def autoscroll(sbar, first, last):
- """hide and show scrollbar as desired"""
- first, last = float(first), float(last)
- if first <= 0 and last >= 1:
- sbar.grid_remove()
- else:
- sbar.grid()
- sbar.set(first, last)
- def getImage():
- global imageWhere
- theImage = ImageTk.PhotoImage(Image.open(imageWhere))
- return theImage
- root = Tk()
- hbar = Scrollbar(root, orient=HORIZONTAL)
- vbar = Scrollbar(root, orient=VERTICAL)
- canvas = Canvas(root, scrollregion=(0, 0, 500, 500), yscrollcommand=lambda f, l: autoscroll(vbar, f, l), xscrollcommand=lambda f, l:autoscroll(hbar, f, l))
- hbar['command'] = canvas.xview
- vbar['command'] = canvas.yview
- imageWhere = r'C:\tests4python\imgs\test320x320.png'
- theImage = getImage()
- canvas.create_image(0,0,image=theImage, anchor='nw')
- canvas.grid(column=0, row=0, sticky=(N,W,E,S))
- canvas.bind_all('<MouseWheel>', on_vertical)
- canvas.bind_all('<Shift-MouseWheel>', on_horizontal)
- hbar.grid(column=0, row=1, sticky=(W,E))
- vbar.grid(column=1, row=0, sticky=(N,S))
- root.grid_columnconfigure(0, weight=1)
- root.grid_rowconfigure(0, weight=1)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement