[英]Instantiation of Kivy Children Widgets between Python and Kivy Language
I am having issues getting Kivy to instantiate children Widgets. 我在让Kivy实例化子代小部件时遇到问题。 The issue here I believe is with how I am setting up the root widget.
我认为这里的问题与我如何设置根窗口小部件有关。 According to many examples I have seen including kivy's own documentation I should be able to instantiate children widgets to a root widget in the .kv file without using <> as such:
根据我看到的许多示例(包括kivy自己的文档),我应该能够将子窗口小部件实例化为.kv文件中的根窗口小部件,而无需使用<>这样:
initApp.kv root_rule: initApp.kv root_rule:
Root_Widget:
Test_Screen:
<Test_Screen>:
BoxLayout:
*there is stuff here, leaving blank to keep this question shorter*
Python File: (Note that the add_widget() is commented out) Python文件:(请注意,add_widget()已被注释掉)
class Test_Screen(Screen):
pass
class Root_Widget(ScreenManager):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# self.add_widget(Test_Screen())
pass
class InitApp(App):
def build(self):
return Root_Widget()
if __name__ == '__main__':
InitApp().run()
However, this only leads to a blank app. 但是,这只会导致应用空白。 There are two ways to fix this:
有两种方法可以解决此问题:
initApp.kv class_rule: initApp.kv class_rule:
<Root_Widget>:
Test_Screen:
<Test_Screen>:
BoxLayout:
*there is stuff here, leaving blank to keep this question shorter*
Question 题
My question is, what is the difference here? 我的问题是,这有什么区别? Nesting Test_Screen underneath Root_Widget in the .kv file should be the exact same as calling the add_widget() method correct?
在.kv文件中的Root_Widget下嵌套Test_Screen应该与正确调用add_widget()方法完全相同吗? Is this true and if so how/why is it different when setting Root_Widget as a class rule VS.
这是真的吗?如果是这样,将Root_Widget设置为类规则VS时,为什么/为什么不同? a root rule?
根本规则?
Are there unforeseen consequences to saying that Root_Widget is a class rule rather than a root rule? 说Root_Widget是类规则而不是根规则会产生无法预料的后果吗? It seems to work just fine using this approach but I don't know if it will cause other trouble as the app becomes more complex.
使用这种方法似乎可以正常工作,但是我不知道随着应用程序变得更加复杂,是否还会引起其他麻烦。
The blank window/app could be due to one of the following: 空白的窗口/应用可能是由于以下原因之一:
class InitApp(App):
then kv filename should be init.kv
. class InitApp(App):
那么kv文件名应为init.kv
If you are not using Builder to load kv then the kv file name should be init.kv and not InitApp.kv kv filename - By name convention kv文件名-按名称约定
By name convention
按惯例
Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with 'App' eg:
Kivy以小写形式查找与您的App类同名的Kv文件,如果以'App'结尾则减去“ App”,例如:
MyApp -> my.kv
Instantiate and add a Label widget under Test_Screen as follow: 实例化并在Test_Screen下添加Label小部件,如下所示:
<Test_Screen>:
BoxLayout:
Label:
text: "kv file - Root Rule vs Class Rule"
what is the difference here?
这里有什么区别? Nesting Test_Screen underneath Root_Widget in the kv file should be the exact same as calling the add_widget() method correct?
在kv文件的Root_Widget下嵌套Test_Screen应该与正确调用add_widget()方法完全相同吗?
Yes it is the same. 是的,是一样的。
add_widget(Test_Screen())
function to instantiate add children widgets under the parent. add_widget(Test_Screen())
函数实例化父项下的添加子项小部件。 Test_Screen:
Test_Screen:
实例化并在父Test_Screen:
下添加子级小部件Test_Screen:
add_widget(Button())
to dynamically instantiate and add children widgets to a ScrollView. add_widget(Button())
动态实例add_widget(Button())
控件并将其添加到ScrollView。 Is this true and if so how/why is it different when setting Root_Widget as a class rule VS.
这是真的吗?如果是这样,将Root_Widget设置为类规则VS时,为什么/为什么不同? a root rule?
根本规则?
When the kv file is parsed, the root rule will be set as the root attribute of the App instance. 解析kv文件时,根规则将设置为App实例的根属性。 Whereas a class rule, defines how any instance of that class will be graphically represented.
而类规则定义了如何以图形方式表示该类的任何实例。
Are there unforeseen consequences to saying that Root_Widget is a class rule rather than a root rule? 说Root_Widget是类规则而不是根规则会产生无法预料的后果吗?
I don't believe there is any unforeseen consequences. 我不相信有任何不可预见的后果。 The only thing to note when using root rule or class rule in kv file are as follow.
在kv文件中使用根规则或类规则时,要注意的唯一事项如下。
If in your kv file, you have a root rule defined ie Root_Widget:
then in your Python code, you can do one of the following: 如果在kv文件中定义了根规则,即
Root_Widget:
则在Python代码中,您可以执行以下操作之一:
The kv file is init.kv kv文件是init.kv
class InitApp(App):
pass
The kv file is not init.kv but InitApp.kv kv文件不是init.kv,而是InitApp.kv
class InitApp(App):
def build(self):
return Builder.load_file("InitApp.kv")
If in your kv file, you have a class rule defined ie <Root_Widget>:
then in your Python code, you can do one of the following: 如果在kv文件中定义了一个类规则,即
<Root_Widget>:
那么在Python代码中,您可以执行以下操作之一:
The kv file is init.kv kv文件是init.kv
class InitApp(App):
def build(self):
return Root_Widget()
The kv file is not init.kv but InitApp.kv kv文件不是init.kv,而是InitApp.kv
class InitApp(App):
def build(self):
Builder.load_file("InitApp.kv")
return Root_Widget()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.