用鼠标移动和缩放tkinter画布 [英] Move and zoom a tkinter canvas with mouse

查看:699
本文介绍了用鼠标移动和缩放tkinter画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我想要的描述:
在tkinter画布中绘制几何对象(这里是矩形)的集合,并使用鼠标探索这个画布。点击并拖动移动画布,滚动放大和缩小。



使用此主题,我发现单击并拖动部分:
使用鼠标移动tkinter画布
with-mouse



我设法写一些东西滚动缩放。
移动和缩放工作分开。



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



任何建议?



这里是一段测试代码



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

  import Tkinter as tk 
import random

类示例(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 =horizo​​ntal 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)

#绘制一些矩形
for n in range(50):
x0 = random.randint(0,900)
y0 = random.randint(50,900)
x1 = x0 + random.randint 50,100)
y1 = y0 + random.randint(50,100)
color =(red,orange,yellow,green,blue)[random.randint 0,4)]
self.canvas.create_rectangle(x0,y0,x1,y1,outline =black,fill = color,activefill =black,tags = n)
self.canvas .create_text(50,10,anchor =nw,text =点击并拖动以移动canvas \\\
Scroll进行缩放。)

#这是什么启用使用鼠标:
self.canvas.bind(< ButtonPress-1>,self.move_start)
self.canvas.bind(< B1-Motion>,self.move_move)
#linux scroll
self.canvas.bind(< Button-4>,self.zoomerP)
self.canvas.bind(< Button-5>,self.zoomerM)
#windows scroll
self.canvas.bind(< MouseWheel>,self.zoomer)

#move
def move_start(self,event):
self.canvas.scan_mark(event.x,event.y)
def move_move(self,event):
self.canvas.scan_dragto(event.x,event.y,gain = 1)

#windows zoom
def zoomer(self,event):
if(event.delta> 0):
self.canvas.scale(all,event.x,event.y,1.1,1.1)
elif(event.delta< 0):
self.canvas .scale(all,event.x,event.y,0.9,0.9)
self.canvas.configure(scrollregion = self.canvas.bbox(all))

#linux zoom
def zoomerP(self,event):
self.canvas.scale(all,event.x,event.y,1.1,1.1)
self.canvas.configure (all,event.x,event.y,0.9,event,event, 0.9)
self.canvas.configure(scrollregion = self.canvas.bbox(all))

如果__name__ ==__main__:
root = tk.Tk ()
示例(root).pack(fill =both,expand = True)
root.mainloop()

解决方案

鼠标事件在屏幕坐标中报告。当你有一个滚动的
画布,你经常需要将这些数字转换为'画布(即scrollregion)
坐标'。



缩放焦点:

  true_x = canvas.canvasx(event.x)
true_y = canvas.canvasy .y)


Here a description of what I would like : Draw a collection of geometric objects (here, rectangles) in a tkinter canvas, and beeing abble to explore this canvas using mouse. Click and drag move the canvas, scrolling zooms in and zooms out.

Using this topic, I found the click and drag part : Move a tkinter canvas with Mouse with-mouse

I managed to write something for scrolling zoom. Both moving and zooming work well separatly.

The problem : If I move and then zoom in, the focus of the zoom is not anymore the position of the cursor.

Any suggestion ?

Here a piece of code to test

[edit : should now works for linux and 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)
            x1 = x0 + random.randint(50, 100)
            y1 = y0 + random.randint(50,100)
            color = ("red", "orange", "yellow", "green", "blue")[random.randint(0,4)]
            self.canvas.create_rectangle(x0,y0,x1,y1, outline="black", fill=color, activefill="black", tags=n)
        self.canvas.create_text(50,10, anchor="nw", text="Click and drag to move the canvas\nScroll to zoom.")

        # This is what enables using the mouse:
        self.canvas.bind("<ButtonPress-1>", self.move_start)
        self.canvas.bind("<B1-Motion>", self.move_move)
        #linux scroll
        self.canvas.bind("<Button-4>", self.zoomerP)
        self.canvas.bind("<Button-5>", self.zoomerM)
        #windows scroll
        self.canvas.bind("<MouseWheel>",self.zoomer)

    #move
    def move_start(self, event):
        self.canvas.scan_mark(event.x, event.y)
    def move_move(self, event):
        self.canvas.scan_dragto(event.x, event.y, gain=1)

    #windows zoom
    def zoomer(self,event):
        if (event.delta > 0):
            self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
        elif (event.delta < 0):
            self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

    #linux zoom
    def zoomerP(self,event):
        self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))
    def zoomerM(self,event):
        self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

解决方案

Mouse events are reported in 'screen coordinates'. When you have a scrolled canvas, you often need to convert those numbers to 'canvas (ie. scrollregion) coordinates'.

eg. for your zoom focus:

true_x = canvas.canvasx(event.x)
true_y = canvas.canvasy(event.y)

这篇关于用鼠标移动和缩放tkinter画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆