button background image transparency

There’s a couple of complex things going on here.

First, you should understand how transparency works with Panda. Simply having an alpha channel does not automatically imply transparency; you can turn transparency on and off independently of the alpha channel. (If you load an texture up from an egg file, the egg loader automatically detects the presence of an alpha channel, and assumes that means you meant to have transparency turned on, so it turns it on for you–that’s why a lot of the time you don’t have to think about this.)

But when you pass a texture image name to the DirectButton class, it has no idea whether the texture you give it has an alpha channel or not, so it doesn’t set up transparency by default. But you can always enable transparency after you have created your button like this:


self.button.setTransparency(1)

Now in your second example, when you create a DirectButton with no parameters, you have created a DirectButton that has no contents, and therefore doesn’t take up any space on the screen. When you then add the image separately, it doesn’t update the size of its clickable region. That’s why you can’t click on the button in your second example–it’s really a very tiny button, though it looks big. That’s arguably a bug, but you can work around it by calling this:


self.button.setFrameSize()

This doesn’t explain why transparency works in the second example but not in the first. In fact, I don’t understand that at all; there’s no reason why transparency should work in the second example either, if you haven’t explicitly turned it on. Maybe you are doing something slightly different with the button in that case; for instance, maybe you are parenting it to a frame that does have transparency enabled, and it is inheriting that transparency setting.

Finally, although you can pass just the name of a texture to the DirectButton class and it will load the texture, it is generally better to load a texture up from an egg file instead of loading it directly by filename. This gets several nice benefits, including the automatic detection of an alpha channel and automatic enabling of transparency (as mentioned above).

To do this, make an egg file that contains a single polygon with your texture applied to it. If you don’t have one, you can make one with this command line:


egg-texture-cards -o mycard.egg test.tif

Then you can load this and use it as the background of your button, like this:

card = loader.loadModel('models/mycard.egg')
self.button = DirectButton(self.frame, image=card.find('test'))

That obviates the need to explicitly enable transparency.

David