Toggling a button

How would you put a button in the ‘down’ state on the first click and in the ‘normal’ state on the second click? And would you make this out of a DirectButton or a DirectCheckButton? This is what I’ve tried so far.

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

class Core(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        ButtonBar()


class ButtonBar():
    def __init__(self):
        self.makebtns()

    def makebtns(self):
        self.btnsr = DirectButton(command=self.toggle)
        self.btnsr['extraArgs'] = [self.btnsr]

    def toggle(self,me):
        if me['state'] == 'normal':
            me['state'] = 'down'


core = Core()
core.run()

Changing the state to ‘down’ freezes the Button because its not a valid state.

Hmm… I’m not sure that DirectButton’s “state” field is what you’re looking for, but rather the “setState” method of PGItem. Either way, the appropriate state isn’t “down”, but rather “DGG.BUTTON_DEPRESSED_STATE” (which has the value “1”). Similarly, the “ready” (i.e. unpressed) state is “DGG.BUTTON_READY_STATE” (which has the value “0”).

In short, you might try a command like this:

myDirectButton.node().setState(DGG.BUTTON_DEPRESSED_STATE)

You’re a legend mate! the reference manual is not easy to navigate around for a beginner so your help is much appreciated

It’s my pleasure! :slight_smile:

Panda has a great many elements to it, so it can sometimes be difficult to find what one is looking for–especially for things like this, that likely aren’t part of the intended use of the widgets in question.