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

MKP

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

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

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

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

#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
肯莉

要将函数与参数绑定,请使用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的解释

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

tkinter 画布重叠/阻止鼠标单击事件

来自分类Dev

鼠标单击并按住事件?

来自分类Dev

套接字io鼠标单击事件

来自分类Dev

如何创建鼠标单击事件序列

来自分类Dev

通过形状传递鼠标单击事件

来自分类Dev

如何打破鼠标单击事件的循环

来自分类Dev

鼠标单击事件后刷新绘图

来自分类Dev

在Matplotlib中,在发生鼠标单击事件时,填充鼠标单击右侧的图

来自分类Dev

单击表单下方的元素的鼠标单击触发事件

来自分类Dev

如何从鼠标单击事件中调用绘画事件?

来自分类Dev

Python脚本来控制鼠标单击

来自分类Dev

Python 3.5自动鼠标单击

来自分类Dev

检测鼠标单击是在元素之前还是之后

来自分类Dev

在鼠标单击之前,JPanel不会绘制图像

来自分类Dev

侦听iframe中的鼠标单击和按键事件

来自分类Dev

使用asyncio对matplotlib中的鼠标单击事件做出反应

来自分类Dev

如何在鼠标单击时设置可拖动事件?

来自分类Dev

在C#Silverlight中从鼠标单击事件收集分数

来自分类Dev

防止鼠标单击期间发生键盘事件(未发布)

来自分类Dev

使用matplotlib存储鼠标单击事件坐标

来自分类Dev

是否可以使用Javascript触发鼠标单击事件?

来自分类Dev

Flash ActionScript 3功能需要鼠标单击事件吗?

来自分类Dev

如何阻止其他形式的鼠标单击事件

来自分类Dev

在鼠标单击的事件中重置散点图的节点颜色

来自分类Dev

如何抑制Windows中的全局鼠标单击事件?

来自分类Dev

在ListBox上发布选择鼠标单击事件

来自分类Dev

中断所有mousedown事件以模拟鼠标单击

来自分类Dev

Java:在鼠标单击事件上启动线程

来自分类Dev

如何启用透明的QGraphicsPixmapItem来接收鼠标单击事件?

Related 相关文章

热门标签

归档