Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将kv文件中的标签文本绑定到python文件中的方法_Python 3.x_Text_Label_Kivy_Kivy Language - Fatal编程技术网

Python 3.x 将kv文件中的标签文本绑定到python文件中的方法

Python 3.x 将kv文件中的标签文本绑定到python文件中的方法,python-3.x,text,label,kivy,kivy-language,Python 3.x,Text,Label,Kivy,Kivy Language,我想用简单的话将.kv文件中的函数绑定到.py文件中的方法我想每天在我的应用程序的主屏幕上更新引号,所以我使用屏幕小部件来显示引号,那么如何将label.text绑定到.py文件中的方法 下面是我要绑定的.py文件类 class MainScreen(FloatLayout): def quote(self): self.quote.text = 'I often think that the night is more alive and more richly col

我想用简单的话将.kv文件中的函数绑定到.py文件中的方法我想每天在我的应用程序的主屏幕上更新引号,所以我使用屏幕小部件来显示引号,那么如何将label.text绑定到.py文件中的方法 下面是我要绑定的.py文件类

class MainScreen(FloatLayout):
    def quote(self):
        self.quote.text = 'I often think that the night is more alive and more richly colored than 
                             the day. - Vincent van Gogh'
下面是.kv文件的根小部件

    <MainScreen>:
        story: story

        canvas:
            Color:
                rgba: 0, 0, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size

        Label:
            id: story
            text: root.quote
            font_size: '20sp'
            size: self.texture_size
            pos_hint: {'x': -0.2, 'y': 0.27}
:
故事:故事
画布:
颜色:
rgba:0,0,0,1
矩形:
pos:self.pos
大小:self.size
标签:
id:故事
文本:root.quote
字体大小:“20sp”
大小:self.texture\u大小
位置提示:{'x':-0.2,'y':0.27}

但是它显示的错误是label.text必须是string

您可以使用kivy
属性
,并使用
quote()
方法更新该
属性
。下面是一种在代码中实现这一点的方法:

class MainScreen(FloatLayout):
    quote_text = StringProperty('Not Set')

    def quote(self, *args):
        self.quote_text = 'I often think that the night is more alive and more richly colored than  the day. - Vincent van Gogh'
quote\u text
是一个
StringProperty
,可以在
kv
中引用,而
quote
方法现在更新该
StringProperty

在您的
kv

<MainScreen>:
    story: story

    canvas:
        Color:
            rgba: 0, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size

    Label:
        id: story
        text: root.quote_text
        font_size: '20sp'
        size: self.texture_size
        pos_hint: {'x': -0.2, 'y': 0.27}

您可以使用kivy
属性
,并使用
quote()
方法更新该
属性
。下面是一种在代码中实现这一点的方法:

class MainScreen(FloatLayout):
    quote_text = StringProperty('Not Set')

    def quote(self, *args):
        self.quote_text = 'I often think that the night is more alive and more richly colored than  the day. - Vincent van Gogh'
quote\u text
是一个
StringProperty
,可以在
kv
中引用,而
quote
方法现在更新该
StringProperty

在您的
kv

<MainScreen>:
    story: story

    canvas:
        Color:
            rgba: 0, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size

    Label:
        id: story
        text: root.quote_text
        font_size: '20sp'
        size: self.texture_size
        pos_hint: {'x': -0.2, 'y': 0.27}

这回答了你的问题吗?这回答了你的问题吗?你能再解释一下吗?因为我试过这个,在我传入StringProperty()的屏幕上只显示了“Not Set”(未设置)。请仔细查看我的答案,并在看到
quote\u text
的地方记下。确保您的代码具有相同的更改。如果我在我的mdapp类中返回main,我的样式将不会出现,那么在看不到更多代码的情况下很难判断发生了什么。这就是为什么我们通常要求你发布一个。你能解释一下吗?因为我尝试了这个,在我传入StringProperty()的屏幕上只显示了“Not Set”(未设置)。请仔细查看我的答案,并在看到
quote\u text
的地方记下。确保您的代码具有相同的更改。如果我在我的mdapp类中返回main,我的样式将不会出现,那么在看不到更多代码的情况下很难判断发生了什么。这就是为什么我们通常要求你发表一篇文章。