bind(“<button-2>”,..)不能在tk.canvas上运行

时间:2016-08-24 12:44:59

标签: python tkinter tkinter-canvas

我正在使用MonsterBatDoppelgänger在Move and zoom a tkinter canvas with mouse

中显示的代码

效果很好!但是当我添加一个self.canvas.bind(“<Button-2&gt;”,self.find_nearest)时,右键单击被完全忽略。

以下是修改后的代码:

#!/usr/bin/python2.7
"""
https://stackoverflow.com/questions/25787523/move-and-zoom-a-tkinter-canvas-with-mouse
by MonsterBat Doppelganger
"""
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)
    """ My add """
    # find my position
    self.canvas.bind("<Button-2>", self.find_nearest)

#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"))

""" The rest of the story """
def find_nearest(self,event):
    print 'nearest to %d,%d' % (event.x, event.y)
    closest = self.canvas.find_closest(event.x,event.y)
    if len(closest) == 0:
        print 'Nothing near'
    else:
        print 'Id %s' % closest

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

我甚至没有进入find_nearest的印刷品。一切都像原版一样运行。画布滚动和缩放。没有例外。

1 个答案:

答案 0 :(得分:1)

  

...当我添加... <Button-2> ...时,完全忽略右键单击。

似乎Button-2中间鼠标按钮(即按下滚轮)。对于 right 鼠标按钮,您必须使用Button-3。将您的行更改为此行,它应该有效:

    # find my position
    self.canvas.bind("<Button-3>", self.find_nearest)

请参阅e.g. here以获取参考:

  

通常的设置左侧是按钮1,右侧是按钮3,但左撇子可以交换这些位置。