Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách xác định vị trí cửa sổ Toplevel trong Tkinter

Dùng winfo_x() và winfo_y() của Toplevel để lấy tọa độ. Ví dụ đặt button giữa Toplevel bằng place(relx=0.5, rely=0.5, anchor=CENTER).

Ví dụ

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("750x350")
win.title("Main Window")

def toplevel_position():
    print("The coordinates of Toplevel window are:", top.winfo_x(), top.winfo_y())

top = Toplevel(win, height=150, width=300)
top.title("This is the Toplevel Window")
top.attributes('-topmost', 'true')

ttk.Button(top, text="Get position", command=toplevel_position).place(relx=0.5, rely=0.5, anchor=CENTER)

top.mainloop()

Kết quả

Click "Get position" in ra console tọa độ Toplevel.

Cách xác định vị trí cửa sổ Toplevel trong Tkinter

The coordinates of Toplevel window are: 282 105