简体   繁体   English

Python和Kivy语言之间的Kivy儿童小部件的实例化

[英]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: 有两种方法可以解决此问题:

  1. Uncomment the self.add_widget(Test_Screen()) line OR 取消注释self.add_widget(Test_Screen())行或
  2. Add <> to around the Root_Widget in the .kv file so it would then be a class rule: 在.kv文件中的Root_Widget周围添加<>,这样它将成为一个类规则:

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. 使用这种方法似乎可以正常工作,但是我不知道随着应用程序变得更加复杂,是否还会引起其他麻烦。

Blank Window/App 空白窗口/应用

The blank window/app could be due to one of the following: 空白的窗口/应用可能是由于以下原因之一:

  • kv file name mismatch eg app class name, class InitApp(App): then kv filename should be init.kv . 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 如果未使用Builder加载kv,则kv文件名应为init.kv,而不应为InitApp.kv
  • No root widget in the return statement return语句中没有根小部件
  • No root rule in the kv file 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 

Solution - Blank Window/App 解决方案-空白窗口/应用

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"

Question 1 问题1

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()方法完全相同吗?

Answer 1 答案1

Yes it is the same. 是的,是一样的。

Kivy App - without kv file Kivy App-无kv文件

  • Use add_widget(Test_Screen()) function to instantiate add children widgets under the parent. 使用add_widget(Test_Screen())函数实例化父项下的添加子项小部件。

Kivy App - with kv file Kivy App-带有KV文件

  • Instantiate and add children widgets under the parent using Test_Screen: 使用Test_Screen:实例化并在父Test_Screen:下添加子级小部件Test_Screen:
  • Can use both eg using add_widget(Button()) to dynamically instantiate and add children widgets to a ScrollView. 可以同时使用,例如,使用add_widget(Button())动态实例add_widget(Button())控件并将其添加到ScrollView。

Question 2 问题2

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? 根本规则?

Answer 2 答案2

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. 而类规则定义了如何以图形方式表示该类的任何实例。

Question 3 问题3

Are there unforeseen consequences to saying that Root_Widget is a class rule rather than a root rule? 说Root_Widget是类规则而不是根规则会产生无法预料的后果吗?

Answer 3 答案3

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文件中使用根规则类规则时,要注意的唯一事项如下。

kv file - Using root rule 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代码中,您可以执行以下操作之一:

Option 1 选项1

The kv file is init.kv kv文件是init.kv

class InitApp(App):
    pass

Option 2 选项2

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")

kv file - Using class rule 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代码中,您可以执行以下操作之一:

Option 1 选项1

The kv file is init.kv kv文件是init.kv

class InitApp(App):

    def build(self):
        return Root_Widget()

Option 2 选项2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM