简体   繁体   中英

How to move a rectangle in x and y pixels position with tkinter?

I want to move my rectangle at specific x and y pixels and not add to it like this function tkinter.Canvas.move . I want to specify an absolute coordinate (x, y) in which my rectangle should placed in. This image will explain. Thanks!

This Image explains every thing

This is test code but it's add to x and y not assign to it:

from tkinter import *

root = Tk()
root.title('press Enter to ASSIGN ')
font_mid = ("arial", 15)
c = Canvas()
c.pack()
def move(x, y):
    c.move(rect, x, y)

rect = c.create_rectangle(50, 50, 100, 100, fill='red', activeoutline='orange')


Label(root, text='X:          ', bg='red', fg="white", font=font_mid).place(x=10,y=10)
x_ent = Entry(root, bd=0, font=font_mid)
x_ent.place(x=35, y=12, width=55)


Label(root, text='Y:          ', bg='green', fg="white", font=font_mid).place(x=100,y=10)
y_ent = Entry(root, bd=0, font=font_mid)
y_ent.place(x=125, y=12, width=55)

x_ent.bind('<Return>', lambda i: move(int(x_ent.get()), int(y_ent.get())))
y_ent.bind('<Return>', lambda i: move(int(x_ent.get()), int(y_ent.get())))

root.mainloop()

Thanks.

As far as i can understand, you want to move the rectangle in a specific area of the window as shown in this image .

For this result, you can add a fix number to the y value so it never goes out of that area.

You can try this:

def move(x, y):
    c.moveto(rect, x, y+45)

You may want to restrict the negative value or it will move out.

Hope you were looking for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM