guiItem.isButtonDown problem


I am using a button in the way below to restart my app.

if self.reStart.guiItem.isButtonDown():
restart code

It seems that sometimes after I click the restart button the restart code runs one time and sometimes run forever. It is like the restart button stays clicked forever.

Is this normal?
Is there a command to say manually to unclick the button?

thank you

Rather than polling isButtonDown(), it would probably be better to take advantage of the “command” keyword of DirectButton to run your function when the button is clicked.

The method isButtonDown() will return true over a series of frames, not just for a single frame; and depending on what you do in response to that event (like removing the button from the screen), it may be possible to lose the mouse-up event that would have set it false, so that it never goes to 0. This method is a low-level method that really isn’t intended to be used in this way.


thank you david for your answer.

You solution works but I have two issues.

I am using the code below to create the button in a class and call a method from another class.

button11 = DirectButton( scale=(0.0215,1,0.028), pos = (0.8,0,0.8), image = (“models/Images/START.png”), relief = None, state = 1, pressEffect = 1, command = ancestor.restartApp())
return button11

basically I want to run a method only when I click the button.
First when I create the button the function in the command runs automatically (not what I want).
Moreover how do I call the method in the command, because when I click the button nothing happens.

thank you

You have an error:

command = ancestor.restartApp() # this means get result of restartApp
needs to change to this
command = ancestor.restartApp # this means get restartApp function itself