簡體   English   中英

如何使用 tkinter 在 x 和 y 像素 position 中移動矩形?

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

我想以特定的 x 和 y 像素移動我的矩形,而不是像這樣添加到它 function tkinter.Canvas.move 我想指定一個絕對坐標(x,y),我的矩形應該放在其中。這張圖片將解釋。 謝謝!

這張圖片解釋了一切

這是測試代碼,但它被添加到 x 並且 y 沒有分配給它:

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()

謝謝。

據我所知,您想在 window 的特定區域移動矩形,如圖所示

對於這個結果,您可以為 y 值添加一個固定數字,使其永遠不會超出該區域。

你可以試試這個:

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

您可能想要限制負值,否則它將移出。

希望你正在尋找這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM