Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีทับซ้อน Widget/Frame ใน Tkinter ด้วย Place Geometry Manager

Geometry Manager ใน Tkinter มี 3 แบบ: pack, grid, place สำหรับการทับซ้อน widget ให้ใช้ place() ซึ่งจัดตำแหน่งด้วยพิกัด x, y แบบสัมพัทธ์หรือสัมบูรณ์

การกำหนดพิกัด x, y เดียวกันให้หลาย widget จะทำให้ทับซ้อนกัน

ตัวอย่างโค้ด

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x350")

frame1 = Frame(win, bg="LightPink1")
Label(frame1, text="Welcome Folks!", font=('Arial 18 bold italic'), background="white").pack(pady=50)
frame1.place(x=260, y=50)

# ปุ่มทับอยู่เหนือ Frame เดียวกันพิกัด
ttk.Button(frame1, text="Button").place(x=260, y=50)

win.mainloop()

ผลลัพธ์

Label และ Button อยู่ในตำแหน่งเดียวกัน ทับซ้อนกัน

วิธีทับซ้อน Widget/Frame ใน Tkinter ด้วย Place Geometry Manager