Reputation: 41
I have Python Tkinter Tab with a label on the second tab. I want to find the label's y and height parameters. Here is what I have done:
#! python
from tkinter import *
from tkinter import ttk
main = Tk()
main.geometry('450x400')
tabControl = ttk.Notebook(main)
tab1 = Frame(tabControl)
tab2 = Frame(tabControl)
tabControl.add(tab1, text ='Tab Heading 1')
tabControl.add(tab2, text ='Tab Heading 2')
tabControl.pack(expand = 1, fill ="both")
Label(tab1,text="Label on Tab 1").place(x=20, y=40)
Label(tab2,text ="Label on Tab 2").pack(padx=20, pady=(0,30))
L = Label(tab2,text ="Another Tab2 Label")
L.pack()
#tab2.pack()
tab2.update()
print ( L.winfo_y(), L.winfo_height() )
main.mainloop()
As the code is written, the print statement prints 0, 1; a clearly incorrect result. If I uncomment tab2.pack(), the print statement gives 51, 21; presumably the correct values for y and height. However, it also disables tab2. The Label on Tab2 moves to where the Heading was and tab2 is inaccessible even using the Tab key. How can I get the label parameters and have a working tab?
Upvotes: 1
Views: 190
Reputation: 53
Ok I have found where is the problem. The problem is'nt the tab2.pack().
In fact, the problem relies on the position of the "L" label when the tab2 is'nt selected. (do you follow me ?) I'm not sure but I think the Label "L" is'nt visible, but it is actually on the screen when you have selected the tab1.
I have confirmed it by trying to print the position after one second after opening the window, and quickly go on the tab2 when opened. And it printed me "51 21".
But if I don't go on the tab2 after one second, it prints me "0 1".
So if you have understood what I have said, you can now try to fix your code ( I can't because I dont know what were your intentions were. Why did you want the position of the "L" label.)
Welcome
Upvotes: 1