通过kivy中的按钮增加变量值

2024-10-02 04:30:31 发布

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

我试图增加变量值,但出现以下错误:

"UnboundLocalError: local variable 'x' referenced before assignment"

有人能帮我吗

.kv:

<InputValuesScr>:
    GridLayout:
        rows:2
        TextInput:
            input_filter: 'float'
            font_size: 50
            text: 'Please, input value of x1'
            id: xval
            multiline: False
            on_touch_down: self.text = ''
        Button:
            text: 'Submit'
            on_press: root.x_changer()

.py:

global x
x = 1

class TypeOfGeometryScr(Screen):
    pass

class SelectDemensionsScr(Screen):

    def submit_dn(self):
        global dn
        dn = self.ids.demensions.text

class InputValuesScr(Screen):

    def x_changer(self):
        x = x
        x += 1
        self.ids.xval.text = 'Please, input value of x' + str(x)


Tags: oftextselfinputvalueondefscreen
1条回答
网友
1楼 · 发布于 2024-10-02 04:30:31

发生这种情况是因为global x在全局范围内,而不是在x_changer()函数lac范围内。为了解决这个问题,你需要在函数的局部范围内移动global x

def x_changer(self):
        global x
        x = x
        x += 1
        self.ids.xval.text = 'Please, input value of x' + str(x)

相关问题 更多 >

    热门问题