如何将字符串从我的 main.py 传递到我的 .kv
How do I pass a string from my main.py to my .kv
我有一个大字符串需要传递到我的 kv 文件中,以便我可以在最终应用程序中将其打印为标签。唯一的问题是我不知道怎么做。我一直在网上寻找,但找不到可以集成到我的代码中的有效解决方案。
这是包含字符串的 class(“lipoNames”是我想作为标签打印的内容):
class RecordData(Screen):
with open("Lipo names.txt") as f:
lineList = f.readlines()
lipoNames = ("".join(map(str, lineList)))
我已经使用构建器在 class 之外打开了 kv 文件,因为我正在使用多个菜单。您将在下面找到我的 kv 文件的一部分,其中将放置标签:
<RecordData>
name: "record"
Label:
text: ???
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint:{"x": 0.325, "y": 0.86}
size_hint:0.35, 0.15
这是我使用 kv 的第一个项目,所以我对使用 kv 文件还是很陌生。
感谢您的帮助!
在此您可以将您的 .kv 文件写入 python 文件的变量中,您可以只使用 f"{}" 来传递您的字符串示例
text = ""
a = f"""
<RecordData>
name: {text}
Label:
text: ???
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint:{"x": 0.325, "y": 0.86}
size_hint:0.35, 0.15
"""
希望对您有所帮助
您可以用 kv 语言调用 root.yourvariable(它的作用类似于 self.yourvariable),然后在您的相对 class 函数中将 Kivy StringProperty 类型调用到您的变量中,这样就不会进行类型转换需要并且 Kivy 知道如何处理变量。
尝试下面的这个功能,它会在您每次单击标签时更新文本:
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.properties import StringProperty
from kivy.lang import Builder
a = """
<RecordData>
Label:
text: root.variabletext
font_size: (root.width**2 + root.height**2) / 13**4
size_hint:0.35, 0.15
pos_hint:{"x": 0.325, "y": 0.86}
"""
Builder.load_string(a)
class RecordData(FloatLayout):
variabletext = StringProperty("example")
num = 0
def on_touch_down(self, touch):
self.variabletext = "We changed "+str(self.num)+" many times"
self.num += 1
class ExampleApp(App):
def build(self):
return RecordData()
if __name__ == '__main__':
ExampleApp().run()
类似地,您可以在 KV 语言中为标签指定一个 id,然后调用该 id 并在您的相对 python class 函数中设置文本 属性,示例如下:
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.lang import Builder
a = """
<RecordData>
Label:
id: variabletext
text: "example"
font_size: (root.width**2 + root.height**2) / 13**4
size_hint:0.35, 0.15
pos_hint:{"x": 0.325, "y": 0.86}
"""
Builder.load_string(a)
class RecordData(FloatLayout):
num = 0
def on_touch_down(self, touch):
self.ids.variabletext.text = "We changed "+str(self.num)+" many times"
self.num += 1
class ExampleApp(App):
def build(self):
return RecordData()
if __name__ == '__main__':
ExampleApp().run()
我有一个大字符串需要传递到我的 kv 文件中,以便我可以在最终应用程序中将其打印为标签。唯一的问题是我不知道怎么做。我一直在网上寻找,但找不到可以集成到我的代码中的有效解决方案。
这是包含字符串的 class(“lipoNames”是我想作为标签打印的内容):
class RecordData(Screen):
with open("Lipo names.txt") as f:
lineList = f.readlines()
lipoNames = ("".join(map(str, lineList)))
我已经使用构建器在 class 之外打开了 kv 文件,因为我正在使用多个菜单。您将在下面找到我的 kv 文件的一部分,其中将放置标签:
<RecordData>
name: "record"
Label:
text: ???
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint:{"x": 0.325, "y": 0.86}
size_hint:0.35, 0.15
这是我使用 kv 的第一个项目,所以我对使用 kv 文件还是很陌生。 感谢您的帮助!
在此您可以将您的 .kv 文件写入 python 文件的变量中,您可以只使用 f"{}" 来传递您的字符串示例
text = ""
a = f"""
<RecordData>
name: {text}
Label:
text: ???
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint:{"x": 0.325, "y": 0.86}
size_hint:0.35, 0.15
"""
希望对您有所帮助
您可以用 kv 语言调用 root.yourvariable(它的作用类似于 self.yourvariable),然后在您的相对 class 函数中将 Kivy StringProperty 类型调用到您的变量中,这样就不会进行类型转换需要并且 Kivy 知道如何处理变量。
尝试下面的这个功能,它会在您每次单击标签时更新文本:
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.properties import StringProperty
from kivy.lang import Builder
a = """
<RecordData>
Label:
text: root.variabletext
font_size: (root.width**2 + root.height**2) / 13**4
size_hint:0.35, 0.15
pos_hint:{"x": 0.325, "y": 0.86}
"""
Builder.load_string(a)
class RecordData(FloatLayout):
variabletext = StringProperty("example")
num = 0
def on_touch_down(self, touch):
self.variabletext = "We changed "+str(self.num)+" many times"
self.num += 1
class ExampleApp(App):
def build(self):
return RecordData()
if __name__ == '__main__':
ExampleApp().run()
类似地,您可以在 KV 语言中为标签指定一个 id,然后调用该 id 并在您的相对 python class 函数中设置文本 属性,示例如下:
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.lang import Builder
a = """
<RecordData>
Label:
id: variabletext
text: "example"
font_size: (root.width**2 + root.height**2) / 13**4
size_hint:0.35, 0.15
pos_hint:{"x": 0.325, "y": 0.86}
"""
Builder.load_string(a)
class RecordData(FloatLayout):
num = 0
def on_touch_down(self, touch):
self.ids.variabletext.text = "We changed "+str(self.num)+" many times"
self.num += 1
class ExampleApp(App):
def build(self):
return RecordData()
if __name__ == '__main__':
ExampleApp().run()