小编Mon*_*ger的帖子

用鼠标移动和缩放 tkinter 画布

这里描述了我想要的内容:在 tkinter 画布中绘制一组几何对象(此处为矩形),然后可以使用鼠标探索该画布。单击并拖动移动画布,滚动放大和缩小。

使用这个主题,我找到了点击和拖动部分: 用鼠标 和鼠标移动 tkinter 画布

我设法为滚动缩放写了一些东西。移动和缩放都可以分别很好地工作。

问题:如果我移动然后放大,放大的焦点不再是光标的位置。

有什么建议吗?

这里有一段代码来测试

[编辑:现在应该适用于 linux 和 windows]

import Tkinter as tk
import random

class Example(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        self.canvas = tk.Canvas(self, width=400, height=400, background="bisque")
        self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set)
        self.canvas.configure(scrollregion=(0,0,1000,1000))

        self.xsb.grid(row=1, column=0, sticky="ew")
        self.ysb.grid(row=0, column=1, sticky="ns")
        self.canvas.grid(row=0, column=0, sticky="nsew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        #Plot some rectangles
        for n in range(50):
            x0 = random.randint(0, 900)
            y0 = random.randint(50, 900) …
Run Code Online (Sandbox Code Playgroud)

python linux canvas tkinter

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

canvas ×1

linux ×1

python ×1

tkinter ×1