鼠标单击前激活的事件(Python,Tkinter)

2024-05-06 18:42:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

到目前为止,我有一些代码可以移动一个形状,但是当我随后尝试移动屏幕上的另一个形状时,这个形状在我可以单击它之前就跳转了。我想现在的情况是,移动形状的代码在没有点击的情况下就被激活了,所以代码中的坐标没有被重置,从而导致形状跳跃(我希望,当第二个形状被点击时,我的代码应该会得到第二个形状的坐标)

我试着在网上寻找它为什么不点击就可以工作的原因,但是没有找到任何东西。我还试着在第一个形状被放置后解开移动形状的标签,这样在第二个形状被点击之前它们不会被绑定,但是这似乎没有起作用。在

有人能解释一下这里发生了什么吗?在

#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

Tags: selfeventmovereturnmaindefwindowb1
1条回答
网友
1楼 · 发布于 2024-05-06 18:42:57

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

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

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

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

相关问题 更多 >