Why does my tkinter object keep changing shape?

35
October 22, 2019, at 9:00 PM

I have created an image on a canvas in tkinter that responds to a button event. And, the object is created on position x and position y where that event took place. But the object changes shape constantly.

def leftclick(event):
        canvas1=Canvas(play, height=hei, width=wid)
        canvas1.grid(row=0, column=0, sticky=W)
        canvas1.delete("all")

        x=event.x
        y=event.y
        print(event.x, event.y)

        bullet = canvas1.create_oval(x,y, 100,100, fill="red")
        xspeed=random.randint(0, 50)
        yspeed=random.randint(0,50)

This just draws ovals which are randomly shaped. Why is this happening and how do I fix it?

Answer 1

You should only create your canvas once, but that's not a problem. The problem is that the tkinter tries to create an oval inside the rectangle. You've specified the 2 points of the rectangle: x,y and 100,100. Just use bullet = canvas1.create_oval(x-50,y-50, x+50,y+50, fill="red") or whatever number you pick instead of 50. Hope that's helpful!

READ ALSO
Order coplanar points for drawing them

Order coplanar points for drawing them

I have an algorithm wich generates from three to six points of intersection between a plane and the edges of a cube which contains that planeFor drawing the plane (which gets drawn a polygon) OpenGL needs the vertex to be ordered as in the following picture:

28
Bytes input is INCORRECT (can't convert to JSON) in Python

Bytes input is INCORRECT (can't convert to JSON) in Python

Basically, I have this API end point that will be called if you make a POST request to itThe problem is for some reason, I can't convert the bytes to JSON so I can access the data

53
Python Email message not producing an output

Python Email message not producing an output

I have to design a class Message that models an e-mail messageA message has a recipient, a sender, and a message text

43
Understanding lambda function inputs in Spark for RDDs

Understanding lambda function inputs in Spark for RDDs

I am doing a course on Spark and I am a bit confused

26