标签文本没有改变

2024-09-28 22:12:52 发布

您现在位置:Python中文网/ 问答频道 /正文

当我按下一个按钮时,我正在尝试更改淡紫色文本,但它不会更改我有以下代码:

控制器:

class Controller(BoxLayout):
    random_string = StringProperty()
    random_string="hola"
    def do_action(self):
        random_string="h22l"
        print(random_string)
    def do_action2(self):
        random_string="hl2332323"
        print(str(random_string))

我的.mk:

<Controller>:
label: lvId

BoxLayout:
    orientation: 'vertical'

    Button:
        text: 'Click Me'
        on_press: root.do_action()

    Button:
        text: 'Click Me'
        on_press: root.do_action2()

    Label:
        id: lvId
        text: root.random_string
        text_size: root.width, None
        size: self.texture_size

Tags: textselfsizestringdefbuttonactionrandom
1条回答
网友
1楼 · 发布于 2024-09-28 22:12:52

必须通过self访问变量,否则,将创建一个新的局部变量,而不是要访问的局部变量。你知道吗

class Controller(BoxLayout):
    random_string = StringProperty("hola")
    def do_action(self):
        self.random_string="h22l"
        print(self.random_string)
    def do_action2(self):
        self.random_string="hl2332323"
        print(self.random_string)

相关问题 更多 >