将变量从 .py 文件传递到 .kv 文件
Pass variable from .py file to .kv file
我是 Kivy 的新手,正在工作中学习。我对如何使用各种小部件和嵌套布局有基本的了解。代码如下(另存为GUI.py):-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ListProperty, NumericProperty, StringProperty
class TestScreen(Screen):
pass
class VariableScreen(Screen):
pass
class SummaryScreen(Screen):
pass
class ProgressScreen(Screen):
pass
class CompletedResultsScreen(Screen):
pass
class SavedResultsScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
GUI_code = Builder.load_file("GUI.kv")
class GUIWindow(App): #App class is inherited
sampletext = StringProperty("Five times Five")
def build(self):
return GUI_code
if __name__ == "__main__":
GUIWindow().run()
GUI.kv 文件包含以下内容:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
TestScreen:
VariableScreen:
SummaryScreen:
ProgressScreen:
CompletedResultsScreen:
SavedResultsScreen:
<TestScreen>:
name: "Test_Screen"
FloatLayout:
Label:
text: "Test"
size_hint: 0.1,0.1
pos_hint: {"right":0.5,"top":1}
Label:
text: app.sampletext
size_hint: 0.1,0.1
pos_hint: {"right":0.1,"top":1}
Button:
on_release: app.root.current = "Saved_Results_Screen"
text: "Saved Results"
size_hint: 0.1,0.1
pos_hint: {"left":1,"bottom":1}
font_size: 15
Button:
on_release: app.root.current = "Variable_Screen"
text: "Variable"
size_hint: 0.1,0.1
pos_hint: {"right":1,"bottom":1}
font_size: 15
只发布了 .kv 文件的相关部分。一些字符串必须从 .py 文件传递到 .kv 文件。该问题已在下面 link:
中得到解决
Pass variable value from main.py to .kv file
根据那里的建议,我使用 StringProperty class 在 GUIWindow class 中放置了示例文本。 (还尝试了一个简单的字符串 sampletext = "Five times Five"。给出了同样的错误)
当第二个标签下的文本属性设置为文本时,代码不会运行:app.sampletext(应用window白色space 打开。它没有响应。需要重新加载 python 内核才能关闭它)
显示如下错误信息
18: pos_hint: {"right":0.5,"top":1}
19: Label:
>> 20: text: app.sampletext
21: size_hint: 0.1,0.1
22: pos_hint: {"right":0.1,"top":1}
...
AttributeError: 'NoneType' object has no attribute 'bind'
当文本 属性 设置为文本时 运行 正确:"Five times Five"
谁能解释一下出了什么问题?
我找不到任何描述原因的文档,但当您解析文件时,解析器似乎正在尝试访问 app.sampletext
,这是您在 App
[=18] 之前所做的=] 连定义都没有,更不用说创建了。
将 Builder.parse
行移动到您的 build(self):
函数中,它将正常工作。
我是 Kivy 的新手,正在工作中学习。我对如何使用各种小部件和嵌套布局有基本的了解。代码如下(另存为GUI.py):-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ListProperty, NumericProperty, StringProperty
class TestScreen(Screen):
pass
class VariableScreen(Screen):
pass
class SummaryScreen(Screen):
pass
class ProgressScreen(Screen):
pass
class CompletedResultsScreen(Screen):
pass
class SavedResultsScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
GUI_code = Builder.load_file("GUI.kv")
class GUIWindow(App): #App class is inherited
sampletext = StringProperty("Five times Five")
def build(self):
return GUI_code
if __name__ == "__main__":
GUIWindow().run()
GUI.kv 文件包含以下内容:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
TestScreen:
VariableScreen:
SummaryScreen:
ProgressScreen:
CompletedResultsScreen:
SavedResultsScreen:
<TestScreen>:
name: "Test_Screen"
FloatLayout:
Label:
text: "Test"
size_hint: 0.1,0.1
pos_hint: {"right":0.5,"top":1}
Label:
text: app.sampletext
size_hint: 0.1,0.1
pos_hint: {"right":0.1,"top":1}
Button:
on_release: app.root.current = "Saved_Results_Screen"
text: "Saved Results"
size_hint: 0.1,0.1
pos_hint: {"left":1,"bottom":1}
font_size: 15
Button:
on_release: app.root.current = "Variable_Screen"
text: "Variable"
size_hint: 0.1,0.1
pos_hint: {"right":1,"bottom":1}
font_size: 15
只发布了 .kv 文件的相关部分。一些字符串必须从 .py 文件传递到 .kv 文件。该问题已在下面 link:
中得到解决Pass variable value from main.py to .kv file
根据那里的建议,我使用 StringProperty class 在 GUIWindow class 中放置了示例文本。 (还尝试了一个简单的字符串 sampletext = "Five times Five"。给出了同样的错误)
当第二个标签下的文本属性设置为文本时,代码不会运行:app.sampletext(应用window白色space 打开。它没有响应。需要重新加载 python 内核才能关闭它)
显示如下错误信息
18: pos_hint: {"right":0.5,"top":1}
19: Label:
>> 20: text: app.sampletext
21: size_hint: 0.1,0.1
22: pos_hint: {"right":0.1,"top":1}
...
AttributeError: 'NoneType' object has no attribute 'bind'
当文本 属性 设置为文本时 运行 正确:"Five times Five"
谁能解释一下出了什么问题?
我找不到任何描述原因的文档,但当您解析文件时,解析器似乎正在尝试访问 app.sampletext
,这是您在 App
[=18] 之前所做的=] 连定义都没有,更不用说创建了。
将 Builder.parse
行移动到您的 build(self):
函数中,它将正常工作。