小编mes*_*her的帖子

更改类中的变量

我正在编写游戏的文本字段类.它应该从类外部将字符串更改为用户输入的字符串.以下是伪代码,但您应该了解我的问题:D

class txtfield(pygame.sprite.Sprite):
    def __init__(self, var, name, x, y):
        self.var=var
        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.var=self.value
            # I want this to be like 'a=self.value'.
            #-> assuming self.name is 'Peter', 'a' should change to 'Peter' 
            #if the user hits enter

a="NAME"
txt=txtfield(a, "popo", 20, 20)
Run Code Online (Sandbox Code Playgroud)

我只是想txtfield改变变量a,但显然这不像我那样工作.

python variables

1
推荐指数
1
解决办法
2523
查看次数

标签 统计

python ×1

variables ×1