This is a Python style issue - my Python code works, I'm just looking for suggestions for a coding convention that will make the code easier to read / understand / debug.
In particular, I am working on a Python class that allows the caller to add widgets to the graphical user interface. To configure the GUI, the user must write a method that adds widgets (named or anonymous) to the widget area so that the widgets form a tree (as is usually the case in graphical interfaces).
So that the user can customize the widget tree without having to specify a name for each widget of the container (and then explicitly refer to this parent widget when adding a child widget), my API supports the concept of a โparent widget stackโ. When declaring a container widget, the user can specify to insert this widget on this stack, and then any additional widgets (which do not explicitly indicate the parent) will be added to the parent at the top of the stack by default. Here is a simple example of what I mean:
def SetupGUI(self): self.AddWidget(name="root", type="container", push=True) self.AddWidget(type="container", push=True) for i in range(0,8): self.AddWidget(name="button%i"%i, type="button") self.PopParentWidget()
This is convenient, but I find that when the hierarchy of the GUI becomes more complex, it becomes difficult to determine what challenge it is for itself. PopParentWidget () corresponds to the widget container. It's easy to insert too much or too little, and you end up with very funny, but unintended results in the GUI.
So my question is to get PopParentWidget () to accept the explicit name of the widgets (which I want to avoid because I don't want to specify each widget in the container), is there anything I can do to make pop pop Is the connection in the code more obvious to the eyes?
In C / C ++, I would do this with indentation, but with Python I am not allowed to do this. For example, I would like to be able to do this:
def SetupGUI(self): self.AddWidget(name="root", type="container", push=True) self.AddWidget(type="container", push=True) for i in range(0,8): self.AddWidget(name="button%i"%i, type="button") self.PopParentWidget()
... but Python will raise an IndentationError if I am so creative.