Tkinter ใช้กันอย่างแพร่หลายในการสร้างแอปพลิเคชันที่ใช้ GUI มีชุดเครื่องมือและฟังก์ชันหรือโมดูลมากมายที่สามารถใช้เพื่อกำหนดคุณลักษณะต่างๆ ของแอปพลิเคชันเฉพาะ สำหรับการสร้างแอปพลิเคชัน GUI มีวิดเจ็ตบางอย่างรวมถึงปุ่ม กล่องข้อความ และป้ายกำกับ เราสามารถปรับแต่งตำแหน่งของวิดเจ็ตและพิกัดบนเฟรม tkinter โดยใช้ฟังก์ชันและไลบรารีอื่นๆ ได้
สมมติว่าเราได้สร้างวิดเจ็ตป้ายกำกับข้อความซึ่งมีตำแหน่งอยู่ในเฟรม tkinter ในการรับพิกัดจริงของวิดเจ็ต เราสามารถใช้เรขาคณิต วิธีการที่มีอยู่ในห้องสมุดของ tkinter
เราจะใช้ winfo_rootx() และ winfo_rooty() ฟังก์ชันที่ส่งคืนพิกัดจริงของวิดเจ็ตตามกรอบหรือหน้าต่าง
ตัวอย่าง
#Import the tkinter library
from tkinter import *
#Create an instance of the tkinter frame
win = Tk()
#Define the geometry of the frame
win.geometry("600x400")
#Define the text-widget
my_text= Text(win, height = 5, width = 52)
# Create label
lab = Label(win, text ="TutorialsPoint.com")
#Configure it using other properties
lab.config(font =("Helvetica", 20))
#Create a button widget
my_button = Button(text="Hello")
#Define the position of the widget
my_button.place(x=100, y=100)
#Update the coordinates with respect to the tkinter frame
win.update()
#Get the coordinates of both text widget and button widget
widget_x1, widget_y1 = my_button.winfo_rootx(),
my_button.winfo_rooty()
widget_x2, widget_y2 = my_text.winfo_rootx(),
my_button.winfo_rooty()
lab.pack()
print(widget_x1, widget_y1)
print(widget_x2, widget_y2)
#Keep the window running
win.mainloop() ผลลัพธ์
การเรียกใช้ข้อมูลโค้ดด้านบนจะพิมพ์ตำแหน่งปัจจุบันของวิดเจ็ตเป็น
134 157 0 157
