My code is the following:
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.title('None')
label = ttk.LabelFrame(window, text = 'What I want to delete')
label.grid(column = 0, row = 0, padx = 5, pady = 5)
text = ttk.Label(label, text = 'Hello World')
text.grid(column = 0, row = 0)
window.mainloop()
Now what surprises me is that when I do the following changes:
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.title('None')
label = ttk.LabelFrame(window, text = 'What I want to delete').grid(column = 0, row = 0, padx = 5, pady=5)
text = ttk.Label(label, text = 'Hello World').grid(column = 0, row = 0)
window.mainloop()
The label's frame does not appear. Only the text. As shown below:
Which means that the LabelFrame is existent, but not shown because there's no error. I think.
In summary, that's the way I "solved it". So, my question is, Is there a fuction that makes It possible not to show the frame in a LabelFrame?
ttk.LabelFrames
are only visible if there is something inside it or if they have a fixed size. In the fisrt example you gave the ttl.Label
widget with text='Hello Word'
is clearly inside the LabelFrame since you passed it as its parent. But in the second example it's not. You may think it is because you also defined label
as the ttk.Label
parent but if you do print(label)
you will see it will print None
and, in tkinter, if you pass None
as a widget master it will understand that the master is the root Tk()
widget.
So, why this happens? The difference between the two examples is that in the first label=ttk.LabelFrame()
which is a LabelFrame object (an instance of the LabelFrame class), while in the second label=ttk.LabelFrame().grid()
which is the output of the grid method, and since the grid method does not return anything label is equal to None
. In conclusion what you are doing is putting the LabelFrame with anything inside and then the second Label, both in the same position of the master window and this is why you can't see the LabelFrame.
Ok, then how to make the LabelFrame invisible? The best option is not using ttk.LabelFrame
but tk.LabelFrame
because now you can disappear with the border using label.configure({"relief":"flat", "text":""})
. Of course this will look like the frame is not there, but everything inside the frame will still be visible. If you want to disappear with things inside the label you can use either label.destroy()
(you will not be able to recover the label) or label.grid_forget() (which will only 'ungrid' the label).