magicgui icon indicating copy to clipboard operation
magicgui copied to clipboard

Nested `guiclass` ?

Open SebastianHambura opened this issue 3 months ago • 1 comments

❓ Questions and Help

Is it possible to have nested @guiclass ?

For example, I would like something like this to work:

from magicgui.experimental import guiclass, button

@guiclass
class A:
    a: int = 0
    b: str = 'hello'

@guiclass
class B:
    inst_a: A = A()
    c: bool = True

    @button
    def greet(self):
        print(f'Greetings! a={self.inst_a.a}, b={self.inst_a.b}, c={self.c}')

The code above fails to run, and I get the following error: ValueError: No widget found for type <class '__main__.A'> and annotation <class '__main__.A'>

What seems to be possible, is to rewrite it like this:

@guiclass
class A:
    a: int = 0
    b: str = 'hello'

@guiclass
class B:
    #inst_a: A = A()
    c: bool = True

    def __init__(self):
        self.inst_a = A()
        self.gui.append(self.inst_a.gui)

    @button
    def greet(self):
        print(f'Greetings! a={self.inst_a.a}, b={self.inst_a.b}, c={self.c}')
Image

I've been looking for a while in the documentation, and I don't think it's written whether it's possible to have this (or not). If not, it would be a cool feature to have!

[x] I have searched the documentation

SebastianHambura avatar Dec 09 '25 16:12 SebastianHambura

Hi @SebastianHambura, yes it's not far off... but you're correct that it's not officially implemented just yet.

There is https://github.com/pyapp-kit/magicgui/pull/704 (which is woefully under-reviewed, my apologies 😢) ... I will try to get to this shortly

tlambert03 avatar Dec 11 '25 16:12 tlambert03