Tkinter 捏缩放

问题描述 投票:0回答:1

我有一个程序,它绘制一个可滚动的画布,上面有一个灰色网格。我需要添加捏缩放功能以支持 MacBook 触摸板。我想在我的画布上添加捏缩放功能。 这是我的代码:

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()

有没有办法用触摸板进行缩放?

python tkinter tkinter-canvas pinchzoom
1个回答
0
投票

正如@tjallo 所说:

core.tcl-lang.org/tips/doc/trunk/tip/570.md 看起来 TKinter 中已经有此功能的功能请求,但尚未原生支持

© www.soinside.com 2019 - 2024. All rights reserved.