Advertisement
here2share

# Tk_image_scrollbars_zoom_and_drag.py ZZZ

Mar 23rd, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. # Tk_image_scrollbars_zoom_and_drag.py ZZZ ??? quite confusing
  2.  
  3. from Tkinter import *
  4. import os
  5.  
  6. import tkFileDialog
  7. from tkFileDialog import askopenfilename
  8.  
  9. import random
  10. from PIL import Image, ImageTk
  11.  
  12. class Cv(): 0
  13. cv=Cv()
  14.  
  15. def hide_scroll(tScrollbar, lo, hi):
  16.     ''' scrollbar hides if it's not needed.
  17.         Works only if you use the grid geometry manager '''
  18.     if float(lo) <= 0.0 and float(hi) >= 1.0:
  19.         grid_remove()
  20.     else:
  21.         grid()
  22.     tScrollbar.set(root, lo, hi)
  23.  
  24. def zoom():
  25.     ''' Simple drag and zoom with mouse wheel '''
  26.  
  27.     def mouse_wheel(event):
  28.         ''' Zoom with mouse wheel '''
  29.         # Respond to Linux (event.num) or Windows (event.delta) wheel event
  30.         if event.num == 5 or event.delta == -120:
  31.             cv.scale += (0.1*cv.scale)
  32.         if event.num == 4 or event.delta == 120:
  33.             cv.scale -= (0.1*cv.scale)
  34.         cv.scale = max(0.001,min(2,cv.scale))
  35.         # Rescale image on canvas
  36.         xi,yi = canvas.coords('photoID')
  37.         xc = canvas.canvasx(event.x)
  38.         yc = canvas.canvasy(event.y)
  39.         x = xc-cv.scale*xc
  40.         y = yc-cv.scale*yc
  41.         canvas.delete('photoID')
  42.         newsize = (int(img.size[0]*cv.scale), int(img.size[1]*cv.scale))
  43.         print newsize
  44.         scaledImg = img.resize(newsize, Image.ANTIALIAS)
  45.         cv.imgTk = ImageTk.PhotoImage(scaledImg)
  46.         canvas.create_image((x, y), anchor='nw', image=cv.imgTk, tag='photoID')
  47.         canvas.lift('info')
  48.         canvas.configure(scrollregion=canvas.bbox("all"))
  49.         # canvas.configure(scrollregion=canvas.bbox(cv.imgTk))
  50.  
  51.     def move_from(event):
  52.         ''' Remember previous coordinates for scrolling with the mouse '''
  53.         canvas.scan_mark(event.x, event.y)
  54.  
  55.     def move_to(event):
  56.         ''' Drag (move) canvas to the new position '''
  57.         if min(event.x, event.y) > 15:
  58.             canvas.scan_dragto(event.x, event.y, gain=1)
  59.        
  60.     # Bind events to the Canvas
  61.     root.bind('<ButtonPress-1>', move_from)
  62.     root.bind('<B1-Motion>',       move_to)
  63.     root.bind('<Button-5>',    mouse_wheel)  # only with Linux, wheel scroll down
  64.     root.bind('<Button-4>',    mouse_wheel)  # only with Linux, wheel scroll up
  65.     root.bind("<MouseWheel>",  mouse_wheel)  # with Windows and MacOS, but not Linux
  66.    
  67.     # Show image and plot some random test rectangles on the canvas
  68.     cv.scale = 1.0
  69.     cv.x, cv.y = 0, 0
  70.  
  71. root = Tk()
  72. ww,hh=500,400
  73. #root.geometry('{}x{}+{}+{}'.format(ww,hh,0,0))
  74. root.title('Simple Zoom With Mouse Wheel')
  75.  
  76. f = askopenfilename(filetypes=[("png files","*.png"),("all files","*.*")])
  77. img = Image.open(f)
  78.  
  79.  
  80. # Lock the canvas size
  81. root.resizable(0,0)
  82.  
  83. frame = Frame(root, bd=2, relief=SUNKEN)
  84.  
  85. frame.grid_rowconfigure(0, weight=1)
  86. frame.grid_columnconfigure(0, weight=1)
  87.  
  88. xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
  89. xscrollbar.grid(row=1, column=0, sticky=E+W)
  90.  
  91. yscrollbar = Scrollbar(frame)
  92. yscrollbar.grid(row=0, column=1, sticky=N+S)
  93.  
  94. canvas = Canvas(frame, bd=0, scrollregion=(0, 0, 1000, 1000),
  95.         width=ww, height=hh,
  96.         xscrollcommand=xscrollbar.set,
  97.         yscrollcommand=yscrollbar.set)
  98. canvas.grid(row=0, column=0, sticky=N+S+E+W)
  99.  
  100.  
  101. cv.imgTk = ImageTk.PhotoImage(img)
  102. canvas.create_image((0, 0), anchor='nw', image=cv.imgTk, tag='photoID')
  103.  
  104. xscrollbar.config(command=canvas.xview)
  105. yscrollbar.config(command=canvas.yview)
  106. canvas.config(scrollregion=canvas.bbox(ALL))
  107.  
  108. canvas.create_text(200, 40, font=('courier', 20, 'bold'), text='Scroll Wheel To Zoom', tag='info')
  109.  
  110. frame.pack()
  111. zoom()
  112.  
  113. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement