如何獲取 Tkinter 畫布中物件座標?
Tkinter 畫布視窗小部件為應用程式提供 GUI 功能。它可用於繪製形狀、動畫物件以及配置畫布中的現有專案。每當建立形狀時,我們必須在畫布專案建構函式中提供形狀的大小和座標。為了返回畫布上某一專案座標,我們可以使用 coords(item) 方法。它返回一個包含畫布視窗小部件中形狀座標的列表。
示例
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of Tkinter frame
win.geometry("700x250")
# Initialize a Canvas Object
canvas = Canvas(win, width= 500, height= 300)
# Draw an oval inside canvas object
c= canvas.create_oval(100,10,410,200, outline= "red", fill= "#adf123")
canvas.pack(expand= True, fill=BOTH)
#Get and Print the coordinates of the Oval
print("Coordinates of the object are:", canvas.coords(c))
win.mainloop()輸出
如果執行上述程式碼,它將在一個視窗中顯示一個橢圓形。

除此之外,程式碼還將返回物件座標並在控制檯上打印出來。
Coordinates of the object are: [100.0, 10.0, 410.0, 200.0]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP