Why is the important order of creating widgets?

The following code is working fine. It displays a panedwindow , with a blue square on top and a green frame below:

 panedwindow .root -orient vertical -showhandle true -background red frame .top -background blue -width 100 -height 100 frame .bot -background green -width 100 -height 100 .root add .top .bot pack .root -expand true -fill both 

However, when I move the panedwindow command, everything stops working. The top blue box is not displayed. Instead, the red panedwindow color shines through:

 frame .top -background blue -width 100 -height 100 panedwindow .root -orient vertical -showhandle true -background red frame .bot -background green -width 100 -height 100 .root add .top .bot pack .root -expand true -fill both 

Why is this happening? Can panedwindow control only widgets that were created after it? I saw a similar behavior with the packer, where he refuses to pack widgets -in widgets that appeared later.

+1
source share
2 answers

All this has to do with styling, as Matt noted. The stacking order is just the "z-coordinate" for widgets in your application. you can think of it as if, in addition to the natural x and y coordinates that you have on the screen, there is another axis perpendicular to the plane of the screen. All widgets are somewhere along this axis. The image you see on the screen is the result of "smoothing" all the widgets together along this axis. Anywhere where widgets overlap in the x and y plane, the widget that is higher in stacking order is what you see in that place.

By default, the storage order of widgets in Tk is determined by the order in which they are created. The earlier the widget that he created, the lower it is in the stacking order - think that the screen itself is at the z-coordinate z, while increasing values ​​get closer to you from the screen. The first widget created has zero stacking order, the second one has one, etc.

The simplest solution to your problem is to simply create the widgets in the correct order, but if you are configured to create widgets in the specified order, you can manually change the stacking order later to make sure the widgets are stacked in the order you want. For example, to add your blue frame to the top again, you can add:

 raise .top .root 

This tells Tk to reorder the .top stacking so that it is β€œabove” .root .

When I use a window widget, I try to control child widgets - for me, this is conceptually only a frame with additional behavior, and since I use frames to group related widgets together, I use the window window in the same way. This policy also neatly wraps up the styling issue, since it requires you to open the window first - you must because you cannot create child elements of the widget until the widget itself is created. Thus, I would modify your example as follows:

 panedwindow .root -orient vertical -showhandle true -background red frame .root.top -background blue -width 100 -height 100 frame .root.bot -background green -width 100 -height 100 .root add .root.top .root.bot 

For me, this makes the connection between .root and .root.top and .root.bot clear: two frames are "inside" on the stack. The natural stacking order is correct and everyone is happy.

+10
source

Wow, that was a long time since I even thought about Tcl / Tk. Thanks for the trip along the memory lane. :)

According to this link , the default storage order for sibling widgets is the order in which they were created, not the order in which they were packaged. In essence, .top, .bot and .root are all siblings of each other because they are on the same level in the hierarchy of widgets, that is, they all "depend" on the same parent (in this case, "."). I expect that if you move the panedwindow command one more line, you will also not see a green frame. I think that if you renamed .top and .bot to .root.top and .root.bot respectively, this could fix the problem you see because it will make them children of the parent .root.

Hope this helps.

+3
source

All Articles