Python 3.x 将kv文件中的标签文本绑定到python文件中的方法
我想用简单的话将.kv文件中的函数绑定到.py文件中的方法我想每天在我的应用程序的主屏幕上更新引号,所以我使用屏幕小部件来显示引号,那么如何将label.text绑定到.py文件中的方法 下面是我要绑定的.py文件类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
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,我的样式将不会出现,那么在看不到更多代码的情况下很难判断发生了什么。这就是为什么我们通常要求你发表一篇文章。