In advance, sorry for the stupid question but can’t figure out the problem!
Why is this code printing the letter ‘z’ in advance and not while pushing the button ?
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.gui.DirectGui import *
class Test(NodePath):
def __init__(self):
z= DirectButton(text = ("z", "z", "z", "disabled"), scale=.11,
command= self.addLetter('z'))
def addLetter(self,data):
print data
Test()
run()
Because when you write self.addLetter(‘z’) it means to call that function right now–at the time you create the DirectButton.
What you really want to write is command = self.addLetterZ, and then define a function called addLetterZ(self) that calls self.addLetter(‘z’).
You can also use a trick called binding, for instance with a lambda syntax, to do this without having to write a separate function, but it’s harder to understand how it works. For the record, the syntax would be command = lambda : self.addLetter(‘z’)