Class Inheriting from DirectButton has no image art

When I try to make a class inheriting from DirectButton, even without methods, it gets created without image. It is not clickable, it is as if it didn’t instantiate properly.

Here is my code:

class ItemButton(DirectButton):
    pass
t = ItemButton(
                image=item.image,
                pos=pos,
                scale=scale,
                command=self.item_click,
                extraArgs=[item],
            )

Results in

image
(The art you’re seeing is the background)

While using the direct button normally:

t = DirectButton(
                image=item.image,
                pos=pos,
                scale=scale,
                command=self.item_click,
                extraArgs=[item],
            )

The result is correct:
image
(The art is placeholder)

Is this a bug, or am I missing something?

In short, sub-classing DirectGUI widgets is… complicated. There are certain methods called during construction that are intended to be called, and pieces of information passed on to super-classes that are expected to be passed on. And I think that this is specific to each individual DirectGUI class–that is, having only the super-class do its part in this isn’t enough.

Thus an “empty” sub-classing may indeed not work as expected, I’m afraid.

(I feel like I’ve seen instructions on how to go about it somewhere, but I don’t seem to be finding them in a quick search, I’m afraid.)

Specifically, I believe that DirectGUI requires a call in each class’s constructor to a method called “initialiseoptions”, passing in a reference to the class itself. It may also require a call to “defineoptions”, passing in any keyword-parameters specific to the sub-class, but I’m not sure about that one.

So a simple sub-classing might look like this:

class ItemButton(DirectButton):
    def __init__(self, parent = None, **kwargs):
        # Let's say that we have no new keyword-parameters to define, and
        # so leave this blank.
        # Again, I'm not sure offhand as to whether this is required.
        optiondefs = ()
        self.defineoptions(kwargs, optiondefs)

        # Note that we >don't< pass in the keyword-parameters here!
        # They should have been handled by the call just above.
        DirectButton.__init__(self, parent)

        # Note that here we pass in a reference to this new class itself,
        # not the current instance of it.
        self.inialiseoptions(ItemButton)
1 Like

Ah, good to know. You misspelled initialiseoptions, but when that is fixed, your solution works!
Thanks so much for the help!

You misspelled initialiseoptions …

Ah, right you are–sorry about that! That’s what I get for typing it out by hand rather than copy-pasting it! ^^;

But I’m glad that the solution works for you, and the help is my pleasure! :slight_smile:

1 Like