DirectButton custom font click and rolling

can we change the text font and the background image when the mouse is on the button and when we click?

        b = DirectButton(text=("Button", "Button", "Button"),
                         image = './mainmenu.png',
                         scale=.05,  relief = None, command=myfunction)

I don’t find in the doc the functions to manage these events.

The font I’m not sure about–it might be possible, but if so, it may involve digging into the components of the button and manually assigning their fonts. I’m not sure.

The background image, however, is fairly straightforward: Just pass in a list of images, one per state. The states and their order should be the same as those given for passing in multiple geoms, as shown in the page that you linked to.

(That is, in the example on that page just replace “geom=” with “image=”, and the various alls to “maps.find” with the paths to your various images, I believe.)

The answer is yes:

b = DirectButton(text=("Button", "Button", "Button"),
                 image=('1.png', '2.png', '3.png'),
1 Like

Ah, I didn’t know about the "text<n>" parameters–that’s neat, and good to know! ^^

thanks rdb it’s work
by digging in the code I found your solution:

If a widget has components: 'text1', 'text2', and 'text3' which all belong
to the 'text' group, they can be all configured with keywords of the form:
'text_keyword' (e.g. ``text_font='comic.rgb'``).

I have a last question with directGui, can my widgets be automatically aligned?
i have two buttons:

from panda3d.core import *
from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectGui import *

class MyApp(ShowBase):

    def __init__(self):
        myFrame = DirectFrame(frameColor=(0, 0, 0, 1),
                              frameSize=(-1, 1, -1, 1))
        myFrame.setPos(-0.5, 0, -0.5)
        b = DirectButton(text=("BTN1"), scale=.05)
        b = DirectButton(text=("BTN2"), scale=.05)

app = MyApp()

I would like the first one to be on top of the other (and not interposed)
I can do this with DirectScrolledFrame and the addItem function but can we do it with a frame?

The simplest, but least convenient approach, is to manually position them via the “pos” keyword-parameter.

However, to have them automatically keep their positions relative to each other, you could parent one below the other. (Taking care to not duplicate scales in the child-widgets, as the scales will compound.) This, combined with simpler use of the “pos” keyword-parameter, should allow you to place one button relative to the other–and to have it keep that relative position even if the other is moved.

Something like this:

button1 = DirectButton(text = "Mew", scale = 0.1)
button2 = DirectButton(text = "Purr", parent = button1, pos = (0, 0, -1.5))


  • The second button has a parent specified, that being the first button
  • The second button has no scale specified–it should inherit its scale from the first button, being a child of the latter.
  • The second button has a position, that being simply 1.5 units downward.
  • If the first button is moved, the second button should move with it.

Oh, you may also be interested in DirectGUI designer, a community-made tool for working with DirectGUI: