我正在嘗試在 Tkinter 網格內制作一個方形跟隨滑鼠游標。
我正在使用此代碼創建一個網格:
for gr in range(0, 400, 10):
canvas.create_line(0,gr, 400,gr, fill='#cfcfcf', width=1)
for gr in range(0,400, 10):
canvas.create_line(gr,0, gr,400, fill='#cfcfcf', width=1)
我將畫布系結到游標“Motion”:
def cursor_move(event):
x, y= event.x, event.y
cursor = canvas.find_withtag('cursor') #cursor is my square which I want to move
canvas.moveto(cursor, newx, newy)
但我不知道如何獲得 newx 和 newy 值。
謝謝你的幫助!
uj5u.com熱心網友回復:
如果您希望正方形對齊網格,請嘗試:
def cursor_move(event):
# calculate grid x and y based on mouse x and y
x, y = event.x//10*10, event.y//10*10
canvas.moveto('cursor', x-1, y-1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/419680.html
標籤:
上一篇:分配兩個Scala類實體
下一篇:從未排序的鏈表中洗掉重復項時出錯
