1

I have this code:

from tkinter import *

root = Tk()

A = Frame(root, highlightbackground="black", highlightthickness=1)
A.grid(row=0)

A0 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=0, column=0)
A1 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=0, column=1)
A2 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=0, column=2)
A3 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=2, column=0)
A4 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=2, column=1)
A5 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=2, column=2)
A6 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=1, column=0)
A7 = Label(A, text="TEST", borderwidth=1, relief="solid", width=15, height=4).grid(row=1, column=2)

B = Frame(A, borderwidth=1, relief="solid").grid(row=1, column=1)
BLabel = Label(B, text="ANOTHER TEST").grid()

C = Frame(root, highlightbackground="black", highlightthickness=1)
C.grid(row=1)
CLabel = Label(C, text="TEST2").grid()

root.mainloop()

I excpect to find BLabel in the midle of the frame A, but instead it is where I would excpect it to be if the it was on the seccond row (row=1) of the root window. What is wrong with my code?

Heikki
  • 341
  • 2
  • 18
  • 3
    `B` is None (the result of the `.grid()` method), NOT the Frame that you appear to expect. Passing None as the first parameter when creating `BLabel` makes it a child of the root window by default, so it's nowhere near the expected position in the widget hierarchy. – jasonharper Sep 07 '21 at 16:44
  • @jasonharper That is it. It seems confusing. Still I have to get clarification: Are Labels A0 to A7 inside Frame A or directly inside root? It would seem that first is true? – Heikki Sep 07 '21 at 17:35
  • 1
    @Heikki Try adding `print(A, B, C)` and you will see what @ jasonharper is talking about. You will see that `B` is `None`. Because of that `tkinter` assumes that you don't want to pass in a mater for `BLabel` so it assumes that you wanted to use `root`. – TheLizzard Sep 07 '21 at 17:41
  • 1
    The seven Labels are indeed inside Frame `A` - since you separated the widget creation from the geometry management call, `A` is a valid reference to the Frame. However, all of the *variables* `A0` thru `A7` are None, since you didn't do the separation for them. (It's not really a problem in those cases, since you made no further use of those variables, but it would have been better to never assign a variable at all in that case.) – jasonharper Sep 07 '21 at 17:59

0 Answers0