【问题标题】:Event activated before mouse click (Python, Tkinter)鼠标单击前激活的事件(Python、Tkinter)
【发布时间】:2016-03-07 10:45:05
【问题描述】:

我正在编写一些代码来移动画布上绘制的形状。这是一个矢量绘图程序,所以我需要能够移动一个形状,然后再移动另一个。

到目前为止,我的代码可以用于移动一个形状,但是当我之后尝试移动屏幕上的另一个形状时,形状会在我点击它之前跳跃。我认为正在发生的事情是无需单击即可激活移动形状的代码,因此代码中的坐标没有被重置,导致形状跳转(我拥有的代码应该获得第二个的坐标点击时的形状,我希望)

出于任何原因,我尝试在线查找它无需单击即可工作,但没有找到任何东西。我还尝试在放置第一个形状后取消绑定移动形状的标签,以便在单击第二个形状之前不会绑定它们,但这似乎不起作用。

谁能解释这里发生了什么?

#code for a two shapes
circle = Main_Window.create_image(500,400, image = CircleIm, tags = "circle")
shape = circle
type1 = Move_Shape(shape)
Main_Window.tag_bind(circle, "<ButtonPress-3>", type1.moveShape(shape))

triangle= Main_Window.create_image(500,400, image = TriangleIm, tags = "triangle")
shape = triangle
type1 = Move_Shape(shape)
Main_Window.tag_bind(triangle, "<ButtonPress-3>", type1.moveShape(shape))


class Move_Shape:
    def __init__(self, shape):
        self.shape = shape
        return

def getShapeType(self, shape):
    global shapeType
    print(self.shape)
    shapeType = Main_Window.gettags(self.shape)  
    return shapeType

def moveShape(self, shape):
    #while left button is held down, we want it to move the tagged shape to     move to the position of the mouse
    global b1, currentX, currentY
    b1 = "down"
    newX, newY = None, None
    shapeType = self.getShapeType(shape)
    print(shapeType)

    Main_Window.addtag_withtag("move_shape", shapeType[0])
    #Bind move_shape to moving code
    print("new tags are", Main_Window.gettags(self.shape))
    Main_Window.tag_bind("move_shape","<Motion>", self.whileMoving)
    Main_Window.tag_bind("move_shape","<ButtonPress-3>", self.getCurrentCoords)
    Main_Window.tag_bind("move_shape", "<ButtonPress-1>", self.startMoving)
    Main_Window.tag_bind("move_shape", "<ButtonRelease-1>", self.stopMoving)
    root_window.mainloop() 
    return shape


def getCurrentCoords(self, event):
    global currentX, currentY
     #make sure the coordinates are obtained before user tries to move shape
    coords = Main_Window.coords(shapeType[0])
    currentX= coords[0]
    currentY = coords[1]
    return currentX, currentY

def startMoving(self,event):
    global b1
    b1 = "down"
    return

def stopMoving(self, event):
    global b1
    b1 = "up"
    newX = None     
    newY = None

    return b1, newX, newY

def whileMoving(self, event):
    global shapeType, b1, currentX, currentY
    if b1 == "down":
        newX = event.x
        newY = event.y
        if newX is not None  and newY is not None:
            x = newX - currentX
            y = newY - currentY
            Main_Window.move(shapeType[0],x,y)
            currentX = newX
            currentY = newY
            newX = event.x
            newY= event.y
        return

【问题讨论】:

    标签: python events tkinter tkinter-canvas


    【解决方案1】:

    要将函数与参数绑定,请使用 lambda 关键字:

    Main_Window.tag_bind(triangle, "<ButtonPress-3>", lambda shape: type1.moveShape(shape))  
    

    如果你使用Main_Window.tag_bind(triangle, "&lt;ButtonPress-3&gt;", type1.moveShape(shape)),Python 会在创建小部件之前调用回调函数,并将函数的返回值传递给 Tkinter。然后 Tkinter 尝试将返回值转换为字符串,并告诉 Tk 在按钮被激活时调用具有该名称的函数。这可能不是您想要的。

    对于这种简单的情况,你可以使用 lambda 表达式作为 Tkinter 和回调函数之间的链接(我拿了effbot.org的这个解释)

    【讨论】:

    • 成功了,谢谢。你能解释一下它在做什么来修复它吗?
    • 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多