Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Zooming in and out with a Tkinter Canvas Widget?
    primarykey
    data
    text
    <p>How would I add zooming in and out to the following script, i'd like to bind it to the mousewheel. If you're testing this script on linux don't forget to change the MouseWheel event to Button-4 and Button-5.</p> <pre><code>from Tkinter import * import Image, ImageTk class GUI: def __init__(self,root): 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) self.canvas = Canvas(frame, bd=0, xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set, xscrollincrement = 10, yscrollincrement = 10) self.canvas.grid(row=0, column=0, sticky=N+S+E+W) File = "PATH TO JPG PICTURE HERE" self.img = ImageTk.PhotoImage(Image.open(File)) self.canvas.create_image(0,0,image=self.img, anchor="nw") self.canvas.config(scrollregion=self.canvas.bbox(ALL)) xscrollbar.config(command=self.canvas.xview) yscrollbar.config(command=self.canvas.yview) frame.pack() self.canvas.bind("&lt;Button 3&gt;",self.grab) self.canvas.bind("&lt;B3-Motion&gt;",self.drag) root.bind("&lt;MouseWheel&gt;",self.zoom) def grab(self,event): self._y = event.y self._x = event.x def drag(self,event): if (self._y-event.y &lt; 0): self.canvas.yview("scroll",-1,"units") elif (self._y-event.y &gt; 0): self.canvas.yview("scroll",1,"units") if (self._x-event.x &lt; 0): self.canvas.xview("scroll",-1,"units") elif (self._x-event.x &gt; 0): self.canvas.xview("scroll",1,"units") self._x = event.x self._y = event.y def zoom(self,event): if event.delta&gt;0: print "ZOOM IN!" elif event.delta&lt;0: print "ZOOM OUT!" root = Tk() GUI(root) root.mainloop() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload