Python string question

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’)

Davud

Aaargh… I understood my error reading your fifth word… I knew it was stupid!!!

I’m actually creating 26 buttons for entering a player name. Isn’t there a call that enable the keyboard writing ?

An other related question:
For a network chat, should I create 26 accept methods in a function or is there something to enable the keyboard ?

Try this thread:
https://discourse.panda3d.org/viewtopic.php?t=1317

David