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 8438649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:46:23+00:00 2026-06-10T07:46:23+00:00

I have been using a GUI library that allows you to connect signals to

  • 0

I have been using a GUI library that allows you to connect signals to signal handlers using a connect function, for example:

widget.connect(signal, callback)

Means that the function callback will be run whenever the signal is fired from the widget. In an attempt to make my code nicer, and remove a series of connect calls from my constructor, I decided to use a decorator, which works well:

def callback(widget, signal)
    def decorate(f):
            widget.connect(signal, f)
            return f
    return decorate

...

@callback(widget, signal)
def do_something():
    ...

This works excellently until I needed to do this in a class – the function is decorated before it is bound to the class, meaning the callback function given doesn’t get the instance of the class that owns it, making it useless. Is there a way to get this to work?

  • 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-06-10T07:46:25+00:00Added an answer on June 10, 2026 at 7:46 am

    A relatively simple solution to this is possible. We begin by using the decorator to mark the function. When instances are constructed, we search for these marks and register the callbacks.

    More specifically, the mark and recapture pattern works by using the decorator to mark the function before it is bound, and then find it amongst the bound methods of the instance in the constructor.

    First we use a decorator to do the marking (we use a set to allow for multiple marks on one method):

    def callback(*args):
        def decorate(f):
            try:
                f._marks.add(args)
            except AttributeError:
                f._marks = {args}
            return f
        return decorate
    

    Then we use the inspect module to find the bound versions of the marked functions, and connect them:

    def connect_callbacks(obj):
        for _, f in inspect.getmembers(obj, inspect.ismethod):
            try:
                marks = f.__func__._marks
            except AttributeError:
                continue
            for widget, signal in marks:
                widget.connect(signal, f)
    

    __func__ is the name of the original, unbound function. This allows us to access the marks that we applied earlier, facilitating our recapture.

    We can then simply create our class and decorate our functions, remembering to connect our callbacks in the constructor:

    class Test:
        def __init__(self):
            ...
            connect_callbacks(self)
    
        @callback(widget, signal)
        def test():
            ...
    

    This allows us to connect the bound methods with a decorator.

    Edit: I have published a tiny library on github that does this for you – it’s called recap.

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

Sidebar

Related Questions

I have recently been using WxPython to create a GUI Network simulator like Cisco
I have been using JMeter in GUI mode for composing all the test cases
I have been using a few cross-platform GUI libraries (such as FLTK, wxWidgets, GTK++),
I have been using Nancy Framework for my C# Applications to create Web-Based GUI's.
I have been playing with using Continuity as a "platform-independent multiuser GUI" for Perl.
What java GUI layout manager does everyone use? Lately, I have been using MigLayout
I have been using netbeans GUI editor for some time now to design a
We have been using the Weka Explorer GUI to build a few classifier models.
I have been using TortoiseSVN for some time and I really like it. I
I have been using Stanford POS Tagger to tag parts of speech in a

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.