简体   繁体   English

kivy屏幕。我必须用super初始化吗?

[英]kivy screens. Do I have to initialize with super?

From the docs : 来自文档

# Declare both screens
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

From this SO question : 从这个SO问题

class WelcomeScreen(Screen):
    def __init__(self, **kwargs):
        super(Screen,self).__init__(**kwargs)

Under which circumstances is it necessary to initialize the screen with super and why ? 在哪种情况下需要用super初始化屏幕,为什么?

Short answer: 简短回答:

No you don't have to use super when defining a Screen. 不,您在定义屏幕时不必使用super。 Yes you do always have to __init__ with super (if you use __init__ ). 是的,你总是必须__init__与super(如果你使用__init__ )。

Longer answer: 更长的回答:

It's nothing unique to screens. 这对屏幕来说并不是唯一的。 In the docs example, you aren't calling __init__ and passing any parameters so no super is necessary. 在docs示例中,您不是在调用__init__并传递任何参数,因此不需要super You don't need an __init__ call to define a Kivy Screen. 您不需要__init__调用来定义Kivy屏幕。 From the SO question, if you're running __init__ you're passing **kwargs up the object hierarchy and also re-defining __init__ which would normally run the initialization of the parent class. 从SO问题来看,如果你正在运行__init__你将** kwargs传递给对象层次结构并重新定义__init__ ,这通常会运行父类的初始化。 super is used to allow you to pass **kwargs and run the parent __init__ without an explicit call to the parent class, in this case, Screen inherits from RelativeLayout , which itself inherits from FloatLayout ; super用于允许你传递** kwargs并运行父__init__而不显式调用父类,在这种情况下, Screen继承自RelativeLayout ,它本身继承自FloatLayout ; without a super call you're overriding the parent class. 如果没有超级电话,你就会覆盖父类。

You don't need to call __init__ unless you have some use for it. 除非你有一些用处,否则你不需要调用__init__ Here is a Screen from one of my apps, with no __init__ call (layout is set in the .kv file): 这是我的一个应用程序的Screen ,没有__init__调用(布局在.kv文件中设置):

class LoginScreen(Screen):
    def login(self):
        self.parent.current = 'ParameterScreen'

You will use __init__ if you want to set the layout, content, properties, etc. of a screen at the moment when you instantiate the class you have created without defining these things in the .kv file; 如果要在实例化已创建的类时设置屏幕的布局,内容,属性等,而不在.kv文件中定义这些内容,则将使用__init__ ; to do that you will also need super as described above. 要做到这一点,你还需要超级如上所述。 It's good practice in kivy though to use the .kv file when you can. 虽然你可以使用.kv文件,但这是kivy的好习惯。

In this example, I want to be able to access BottomBar's parent with self.caller, which I pass in as a **kwargs when I create it, and have this be defined as soon as the bar is instantiated; 在这个例子中,我希望能够使用self.caller访问BottomBar的父级,我在创建它时将其作为** kwargs传入,并在条形图实例化后立即定义; so I need to define it in __init__ which requires a super call. 所以我需要在__init__中定义它,这需要一个超级调用。

class BottomBar(ActionBar):
    titletext = StringProperty('')
    def __init__(self,**kwargs):
        self.caller = kwargs.get('caller')
        super(BottomBar,self).__init__(**kwargs)
    def change_title(self,newtitle):
        self.titletext = newtitle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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