4

Examine the following code:

import Tkinter as tk

root=tk.Tk()
f1 = tk.Frame(width=200, height=200, background="red")
f2 = tk.Frame(width=100, height=100, background="blue")

f1.pack(fill="both", expand=True, padx=20, pady=20)
f2.place(in_=f1, anchor="c", relx=.5, rely=.5)

root.mainloop()

Taken from: How do I center a frame within a frame in Tkinter?

One of the comments there says "The root window is the default when a widget doesn't explicitly specify a parent" and also " In this case f1 is being managed by the root window and f2 is being managed by f1 (because of the in_ parameter)."

What is the difference between the hierarchy created by the master parameter used in the instantiation of new widgets and the hierarchy created by the the in_ prameter used in the .place layout manager?

Why is f2 not being created as a child of f1? (and would it change anything?)

epeleg
  • 10,347
  • 17
  • 101
  • 151
  • The `in_` arg to `.place` (or `.pack` or `.grid`) only affects the _placement_ of the widget. Your `f2` still has `root` as its master, if you do `print(f1.master, f2.master)` you'll see ` . .`, which shows that they both have the root window as their master. – PM 2Ring Oct 02 '17 at 08:35
  • yes, but what is each of the two used for? when would it make sence for an object to to be in_ his master / parent ? – epeleg Oct 02 '17 at 09:52
  • If you want a widget to be placed inside its master then you wouldn't bother with the `in_` arg. You use `in_` when you have a bunch of widgets that you want to treat as one logical group controlled by a single parent, but you want to place them into several different Frames (or other containers). There's a reference for this in the docs, but "in" isn't an easy keyword to search for. ;) If I could find a decent reference I'd write a proper answer... – PM 2Ring Oct 02 '17 at 10:12
  • I though that if i have a frame and want to place two labels in it like pack() would, but using place instead I should be able to do so by placing the second label using label2=Label(fr1, text="Hello world ", bg="red").place(in_=label1, anchor=N, rely=1.0) but it seems to place it based on the height of f1 and not that of label2. – epeleg Oct 02 '17 at 10:23
  • 1
    Sorry, I can't help with that. The `.place` layout method is a bit quirky, and it's generally advised to use `.grid` or `.pack` instead. You can certainly use `in_` with `.grid` or `.pack`, but I don't think there's any way to put another widget inside a Label because it's not a container for widgets, you can only put text or an image into it. – PM 2Ring Oct 02 '17 at 10:35
  • maybe you have a point there in your last statement. I was thinking of "in_" as being "with_respect_to_the_following_widgets_coordinate_system".. what widgets other then frame can be considered valid values for "in_" ? (i.e. containers) ? – epeleg Oct 02 '17 at 10:53
  • no, sorry I got carried away, I am not trying to place stuff in a label nor with respect to it. I will leave this Q trying to get a general answer to parent v.s. in_ and I have created the following Q with a specific example: https://stackoverflow.com/questions/46524030/tkinter-placement-of-widgets where it seems someone has already provided me with an answer i need to check. – epeleg Oct 02 '17 at 10:58
  • 1
    @PM2Ring: yes you can place another widget inside a `Label`. You can also place them inside an `Entry`, a `Checkbutton`, etc. There's rarely a reason to, but tkinter allows it. – Bryan Oakley Oct 02 '17 at 11:53
  • @BryanOakley Interesting! Thanks for that info. – PM 2Ring Oct 02 '17 at 12:47

2 Answers2

2

What is the difference between the hierarchy created by the master parameter used in the instantiation of new widgets and the hierarchy created by the the in_ prameter used in the .place layout manager?

The simple answer is that there is no hierarchy created by the in_ parameter. The only hierarchy is the parent/child relationship when the widget is created.

The in_ parameter merely identifies a widget into which the other widget should be placed. It tells tkinter "make this child's placement relative to this other widget".

Why is f2 not being created as a child of f1? (and would it change anything?)

It's not being created as a child because you didn't tell it to be a child. You didn't specify a parent, so tkinter uses the root window as the parent.

Would it change anything if you did? Not in this specific case. If f1 did not fill the whole window, or filled it asymmetrically, f2 would appear centered in f1 but not centered in the window as a whole.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

My lack of understanding seems to have been the result of a problem in my code for which I got an answer here: tkinter placement of widgets

Getting rid of the issue describe there I can now see that:

Master/parent: defines a bounding area limiting the rendering of its child widgets

in_: sets the widget on which the positioning of the in_(cluded) widget would be based

epeleg
  • 10,347
  • 17
  • 101
  • 151