How to do it…

The labels that we will display have different backgrounds and are defined in the order they are placed from left to right and top to bottom:

import tkinter as tk

class App(tk.Tk):
def __init__(self):
super().__init__()
label_a = tk.Label(self, text="Label A", bg="yellow")
label_b = tk.Label(self, text="Label B", bg="orange")
label_c = tk.Label(self, text="Label C", bg="red")
label_d = tk.Label(self, text="Label D", bg="green")
label_e = tk.Label(self, text="Label E", bg="blue")

label_a.place(relwidth=0.25, relheight=0.25)
label_b.place(x=100, anchor=tk.N,
width=100, height=50)
label_c.place(relx=0.5, rely=0.5, anchor=tk.CENTER,
relwidth=0.5, relheight=0.5)
label_d.place(in_=label_c, anchor=tk.N + tk.W,
x=2, y=2, relx=0.5, rely=0.5,
relwidth=0.5, relheight=0.5)
label_e.place(x=200, y=200, anchor=tk.S + tk.E,
relwidth=0.25, relheight=0.25)

if __name__ == "__main__":
app = App()
app.mainloop()

If you run the preceding program, you can see the overlapping between label_c and label_d in the center of the screen, something that we have not achieved with other geometry managers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset