I have a program, that draws a canvas that is scrollable and has a gray grid on it. I need to add pinch-zooming to support MacBook touchpad. I would like to add pinch-zooming functional to my canvas. Here is my code:
import random
class Editor(tkinter.Tk):
def _on_vertical_scroll(self, event):
print(event)
self.c.yview_scroll(-10 * event.delta, 'units')
def _on_horizontal_scroll(self, event):
print(event)
self.c.xview_scroll(-10 * event.delta, 'units')
def __init__(self, root, filename=None, project_width=10000, project_height=10000):
self.project_width, self.project_height = project_width, project_height
self.filename = filename
if not self.filename:
self.filename = "untitled"
self.root = root
self.root.geometry("+0+0")
self.root.title(f"{self.filename} – LogicEdit")
canvas_frame = tkinter.Frame(self.root, width=self.root.winfo_screenwidth(), height=self.root.winfo_screenheight())
canvas_frame.pack(expand=True, fill=tkinter.BOTH)
self.c = tkinter.Canvas(canvas_frame, bg='#FFFFFF', width=self.root.winfo_screenwidth(), height=self.root.winfo_screenheight(), scrollregion=(0, 0, self.project_width, self.project_height))
self.hbar = tkinter.Scrollbar(canvas_frame, orient=tkinter.HORIZONTAL)
self.hbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
self.hbar.config(command=self.c.xview)
self.vbar = tkinter.Scrollbar(canvas_frame, orient=tkinter.VERTICAL)
self.vbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
self.vbar.config(command=self.c.yview)
self.c.config(width=self.root.winfo_screenwidth(), height=self.root.winfo_screenheight())
self.c.config(xscrollcommand=self.hbar.set, yscrollcommand=self.vbar.set)
self.c.pack(side=tkinter.LEFT, expand=True, fill=tkinter.BOTH)
self.c.config(yscrollincrement = 1, xscrollincrement = 1)
self.c.bind_all('<MouseWheel>', self._on_vertical_scroll)
self.c.bind_all('<Shift-MouseWheel>', self._on_horizontal_scroll)
for x in range(0, 10000, 50):
self.c.create_line(x, 0, x, self.project_height, fill = "#dddddd")
for y in range(0, 10000, 50):
self.c.create_line(0, y, self.project_width, y, fill = "#dddddd")
if __name__ == "__main__":
root = tkinter.Tk()
e = Editor(root)
print(e)
root.mainloop()
Is there a way to make zoom with touchpad?
As @tjallo said:
core.tcl-lang.org/tips/doc/trunk/tip/570.md Looks like there is already a feature request for this feature in TKinter, but it isn't natively supported yet