TurboC
TurboC

Reputation: 918

Tkinter - How can I put a label on a Canvas in order to hide it?

if I try to put a label on a canvas, the latter always stay on the top (see the image attached) but what abut if I want to put the canvas under the label?

enter image description here

below my example code:

from tkinter import *
from tkinter import ttk


def ChangeinItalian():
    MyLabel.configure(text="Ciao Ciao Ciao Ciao:")
def ChangeinEnglish():
    MyLabel.configure(text="Hi Hi Hi Hi:")

root = Tk()
root.geometry("550x350")

MyFrame=Frame(root, background="#ffffff", highlightbackground="#848a98", highlightthickness=1)
MyFrame.place(x=5, y=5, width=535, height=300)

MyFrame.grid_columnconfigure(0, minsize=16)
MyFrame.grid_columnconfigure(2, minsize=10)
MyFrame.grid_columnconfigure(3, weight=1)
MyFrame.grid_columnconfigure(4, minsize=10)
MyFrame.grid_columnconfigure(6, minsize=16)
MyFrame.grid_rowconfigure(0, minsize=16, pad=16)
MyFrame.grid_rowconfigure(2, minsize=16, pad=16)

ttk.Label(MyFrame, text="Introduction", background="#ffffff").grid(row=1, column=1, sticky="w")



# I change the grid method options to cover the label with the canvas.. but it just an example.. what I want to do is put the label on the Canvas and not vice versa.
Canvas(MyFrame, height=1, background="#a0a0a0", highlightthickness=0, highlightbackground="white").grid(row=1, column=1, columnspan=3, sticky="we")



MyLabel=ttk.Label(MyFrame, text="Hi Hi Hi Hi", background="#ffffff")
MyLabel.grid(row=3, column=1, sticky="w")

ttk.Entry(MyFrame, width=70).grid(row=3, column=3)
ttk.Button(MyFrame, text="...", width=5).grid(row=3, column=5, sticky="we")

ItalianButton=ttk.Button(MyFrame, text="Italiano", command=ChangeinItalian)
ItalianButton.place(x=16, y=150)
EnglishButton=ttk.Button(MyFrame, text="English", command=ChangeinEnglish)
EnglishButton.place(x=16, y=200)

root.mainloop()

Upvotes: 1

Views: 1021

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386352

Widgets have what's called a stacking order, which is initially defined by the order in which widgets are created. Widgets created later have a stacking order higher than widgets that are created earlier. When two or more widgets occupy the same space, tkinter uses the stacking order to determine which widget is on top.

In your case you're creating the label and then the canvas, meaning that the canvas has a higher stacking order and thus appears on top of the label.

If you want the canvas under the label, you can either create the canvas before the label, or use the lift or lower method to adjust the stacking order.

Upvotes: 1

Related Questions