Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_image_scrollbars_zoom_and_drag.py ZZZ ??? quite confusing
- from Tkinter import *
- import os
- import tkFileDialog
- from tkFileDialog import askopenfilename
- import random
- from PIL import Image, ImageTk
- class Cv(): 0
- cv=Cv()
- def hide_scroll(tScrollbar, lo, hi):
- ''' scrollbar hides if it's not needed.
- Works only if you use the grid geometry manager '''
- if float(lo) <= 0.0 and float(hi) >= 1.0:
- grid_remove()
- else:
- grid()
- tScrollbar.set(root, lo, hi)
- def zoom():
- ''' Simple drag and zoom with mouse wheel '''
- def mouse_wheel(event):
- ''' Zoom with mouse wheel '''
- # Respond to Linux (event.num) or Windows (event.delta) wheel event
- if event.num == 5 or event.delta == -120:
- cv.scale += (0.1*cv.scale)
- if event.num == 4 or event.delta == 120:
- cv.scale -= (0.1*cv.scale)
- cv.scale = max(0.001,min(2,cv.scale))
- # Rescale image on canvas
- xi,yi = canvas.coords('photoID')
- xc = canvas.canvasx(event.x)
- yc = canvas.canvasy(event.y)
- x = xc-cv.scale*xc
- y = yc-cv.scale*yc
- canvas.delete('photoID')
- newsize = (int(img.size[0]*cv.scale), int(img.size[1]*cv.scale))
- print newsize
- scaledImg = img.resize(newsize, Image.ANTIALIAS)
- cv.imgTk = ImageTk.PhotoImage(scaledImg)
- canvas.create_image((x, y), anchor='nw', image=cv.imgTk, tag='photoID')
- canvas.lift('info')
- canvas.configure(scrollregion=canvas.bbox("all"))
- # canvas.configure(scrollregion=canvas.bbox(cv.imgTk))
- def move_from(event):
- ''' Remember previous coordinates for scrolling with the mouse '''
- canvas.scan_mark(event.x, event.y)
- def move_to(event):
- ''' Drag (move) canvas to the new position '''
- if min(event.x, event.y) > 15:
- canvas.scan_dragto(event.x, event.y, gain=1)
- # Bind events to the Canvas
- root.bind('<ButtonPress-1>', move_from)
- root.bind('<B1-Motion>', move_to)
- root.bind('<Button-5>', mouse_wheel) # only with Linux, wheel scroll down
- root.bind('<Button-4>', mouse_wheel) # only with Linux, wheel scroll up
- root.bind("<MouseWheel>", mouse_wheel) # with Windows and MacOS, but not Linux
- # Show image and plot some random test rectangles on the canvas
- cv.scale = 1.0
- cv.x, cv.y = 0, 0
- root = Tk()
- ww,hh=500,400
- #root.geometry('{}x{}+{}+{}'.format(ww,hh,0,0))
- root.title('Simple Zoom With Mouse Wheel')
- f = askopenfilename(filetypes=[("png files","*.png"),("all files","*.*")])
- img = Image.open(f)
- # Lock the canvas size
- root.resizable(0,0)
- frame = Frame(root, bd=2, relief=SUNKEN)
- frame.grid_rowconfigure(0, weight=1)
- frame.grid_columnconfigure(0, weight=1)
- xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
- xscrollbar.grid(row=1, column=0, sticky=E+W)
- yscrollbar = Scrollbar(frame)
- yscrollbar.grid(row=0, column=1, sticky=N+S)
- canvas = Canvas(frame, bd=0, scrollregion=(0, 0, 1000, 1000),
- width=ww, height=hh,
- xscrollcommand=xscrollbar.set,
- yscrollcommand=yscrollbar.set)
- canvas.grid(row=0, column=0, sticky=N+S+E+W)
- cv.imgTk = ImageTk.PhotoImage(img)
- canvas.create_image((0, 0), anchor='nw', image=cv.imgTk, tag='photoID')
- xscrollbar.config(command=canvas.xview)
- yscrollbar.config(command=canvas.yview)
- canvas.config(scrollregion=canvas.bbox(ALL))
- canvas.create_text(200, 40, font=('courier', 20, 'bold'), text='Scroll Wheel To Zoom', tag='info')
- frame.pack()
- zoom()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement