Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7547667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:30:40+00:00 2026-05-30T09:30:40+00:00

I’m working on a Python class that lets the caller add widgets to a

  • 0

I’m working on a Python class that lets the caller add widgets to a custom GUI. To set up the GUI, the user would write a method that adds widgets (named or anonymous) to the widget area so that the widgets form a tree (as is common in GUIs).

In order to allow the user to set up a tree of widgets without having to give a name to every container widget (and then explicitly reference that parent widget every time a child widget is added), my API supports the notion of a "parent widget stack". When declaring a container widget, the user can specify to push that widget onto this stack, and then any further widgets (that don’t explicitly specify a parent) will be added to the parent at the top of the stack by default. Here’s 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()  # pop the buttons-container off the parents-stack

   self.AddWidget(type="container", push=True)
   for i in range(0,8):
      self.AddWidget(name="slider%i"%i, type="slider")
   self.PopParentWidget()  # pop the sliders-container off the parents-stack

   self.PopParentWidget()  # pop the container "root" off the parents-stack

This is convenient, but I find that when the GUI hierarchy gets more elaborate, it starts to become difficult to tell which call to self.PopParentWidget() corresponds to which container widget. It’s easy to put in one too many, or one too few, and end up with highly amusing but unintended results in the GUI.

So my question is, short of forcing PopParentWidget() to take an explicit widget name (which I want to avoid, because I don’t want to have to name every container widget), is there anything I can do to make the push/pop pairing in the code more apparent to the eye?

In C/C++ I would do it using indentation, but with Python I’m not allowed to do that. For example, I’d love 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()  # pop the buttons-container off the parents-stack
      self.AddWidget(type="container", push=True)
         for i in range(0,8):
            self.AddWidget(name="slider%i"%i, type="slider")
         self.PopParentWidget()  # pop the sliders-container off the parents-stack
      self.PopParentWidget()  # pop the container "root" off the parents-stack

… but Python will throw up an IndentationError if I get creative like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:30:41+00:00Added an answer on May 30, 2026 at 9:30 am

    This sort of situation — where you have a pair of opposite operations — calls for a context manager. Instead of explicitly pushing and popping container widgets onto/from a stack, you would wrap the children of the container in a with block. Building on top of the code you’ve shown here, this could be implemented as something like

    @contextlib.contextmanager
    def container(self, name=None):
        self.AddWidget(name=name, type='container', push=True)
        yield
        self.PopParentWidget()
    

    (Documentation for contextlib.contextmanager).

    Your SetupGUI method then becomes:

    def SetupGUI(self):
        with self.container(name='root'):
            with self.container():
                for i in range(0,8):
                    self.AddWidget(name='button%i' % i, type='button')
            with self.container():
                for i in range(0,8):
                    self.AddWidget(name='slider%i' % i, type='slider')
    

    As you can see, the nesting is clear from the indentation, and there’s no need to manually push and pop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.