简体   繁体   English

我的 RootWidget(ScreenManager) 没有用 kivy/python 初始化我的程序

[英]My RootWidget(ScreenManager) isn't initializing my program with kivy/python

I'm writing a python script with a kv script outside it for formatting.我正在编写一个带有 kv 脚本的 python 脚本,用于格式化。

I have 2 basic screens thus far.到目前为止,我有 2 个基本屏幕。 I can load 1 screen into a preview but things fall apart when I try 2 screens.我可以将 1 个屏幕加载到预览中,但是当我尝试 2 个屏幕时,事情会崩溃。

I get this error:我收到此错误:

kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.

Here's my python code:这是我的python代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

Builder.load_file("design.kv")

class Intro(Screen):
    def go_settings(self):
        self.manager.current = "settings"
    pass

class Settings(Screen):
    print("In settings")
    pass

class RootWidget(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return RootWidget()

if __name__ == "__main__":
    MainApp().run()

My kv code, titled "design.kv":我的 kv 代码,标题为“design.kv”:

<Intro>:
GridLayout:
    cols: 1
    Label:
        text: "Intro"
    Button:
        text: "Settings"
        on_press: root.go_settings()


<Settings>:
    GridLayout:
        cols: 1
        Label:
            text: "Settings"

<RootWidget>:
    Intro:
        name: "intro"
    Settings:              #will work if I comment this line and below
        name: "settings" 

Not sure what's going on.不知道发生了什么。 Thanks for the help!谢谢您的帮助!

Alright, I think I got it.好吧,我想我明白了。

I need to change the names of我需要更改名称

Intro

and

Settings

to

IntroScreen

and

SettingsScreen

It seemed to work for me.它似乎对我有用。 Never thought the name I'd give would matter.从来没有想过我给的名字会很重要。

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

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