Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems what you actually want is a callback:</p> <pre><code>class txtfield(pygame.sprite.Sprite): def __init__(self, ***callback***, name, x, y): ***self.callback=callback*** self.value="" def update(self, key): #I removed image-changing and the code which changes the string self.value #key is a character from the last pressed button #(self.value+=key) if key=="return": ***self.callback(self.value)*** # I want this to be like 'a=self.value'. #-&gt; assuming self.name is 'Peter', 'a' should change to 'Peter' #if the user hits enter ***def myCallback(text): print(text)*** txt=txtfield(***myCallback***, "popo", 20, 20) </code></pre> <p>You can even set variables this way:</p> <pre><code>class MyController(object): def __init__(self): self.A = '' def makeCallback(self): """ Makes a callback which modifies self.A """ def myCallback(text): self.A = text return myCallback controller = MyController() txt = txtfield(controller.makeCallback(), "popo", 20, 20) assert controller.A=='popo' #--&gt; True! </code></pre> <p>The reason you couldn't "set a variable" in your original version was because you told the function "set 'popo' to 'popo'"; the function setting the thing needed to know the name of the object. In the example I gave above, you are saying "set self.A to 'popo'". Here's an example without a class:</p> <pre><code>A = '' def setA(text): A = text txt = txtfield(setA, "popo", 20, 20) assert A=='popo' </code></pre> <p>Here is an example that uses dictionaries (you can also use lists) to avoid having to give a card-coded name:</p> <pre><code>lookupTable = {} def setA(text): lookupTable['A'] = text txt = txtfield(setA, "popo", 20, 20) assert lookupTable['A']=='popo' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload