Constraining a Gui element

Hi everybody, two GUI-related questions:

  1. is there a quick way to constrain a GUI element to, say, 0.1 units from the bottom and 0.2 units from the right side of the window? Or is the only way to intercept some kind of window-resize event and reset the position?

  2. given a GUI element that has been initialized with the parent= option, how do I retrieve the parent from the element? getParent() retrieves a NodePath but this doesn’t have the methods of the parent GUI element, i.e. parent[“frameSize”]


You can reparent the element to base.a2dBottomLeft and offset it with 0.1, 0.2 units.

Thank you pro-soft, that’s perfect!

I probably should restate the second question as it isn’t particularly clear…

Assuming something like:

aFrame = DirectFrame()
aButton = DirectButton(parent=aFrame)

aButton is now a NodePath object parented to aFrame, another NodePath object. Somewhere inside both NodePath object there still are the two GUI objects, but how do I get to them? Something like:

theParentOfButton = aButton[“parent”]

doesn’t work!


It’s usually done by setting a python tag on the gui, pointing to itself. There are already threads about this.

But I like a simpler one, like this :
add this line to your DirectGuiWidget class, in :

self.getWParent = lambda:parent

at the end of its init.

To use it, just call yourWidget.getWParent(), and you’ll get whatever parent it has, whether plain NodePath or DirectGui.

Note that that creates a circular dependency and the object will never get cleaned up unless you clear the tag.
Or, if you’re certain that tag will be there forever, you could call unref() on the node (be very careful though, weird things may happen).

Thank you ynjh_jo for the tip! I’m surprised I didn’t miss a simple method/attribute that stores the information, but adding an ad-hoc one it’s pretty much as simple.

And thank you pro-rsoft for the warning. It makes perfect sense. I’ll make sure to clear it before I let go of the gui element.

Thank you both again!