kivy.uix.screenmanager.ScreenManagerException: ScreenManager 只接受 Screen 小部件。错误

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

我正在尝试将这两个屏幕添加到屏幕管理器,但出现此错误。我究竟做错了什么?我知道还有其他方法可以使用屏幕管理器,但我想知道如何将它与构建器方法一起用于这些单独的 kv 文件。

我为屏幕编写的代码:

Python

from kivy.uix.screenmanager import Screen

class Screen2(Screen):
    def buttFunc2(self):
        print("Screen2")
from kivy.uix.screenmanager import Screen

class Screen1(Screen):
    def buttFunc(self):
        print("Screen1")

KV

<Screen2>:
    name: "screen2"

    BoxLayout:
        Button:
            text: "Screen 2"
            on_release: root.buttFunc2()
        Button:
            text: "Go To Screen1"
            #on_release: i also want this function to change the screen to the other one 
<Screen1>:
    name: "screen1"

    BoxLayout:
        Button:
            text: "Screen 1"
            on_release: root.buttFunc()
        
        Button:
            text: "Go To Screen2"
            #on_release: i also want this function to change the screen to the other one 

Main.py Python 文件:

from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from screen1 import Screen1


class MyApplication(MDApp):
    
    def build(self):
        Window.size = 300,600
        sm = ScreenManager()
        sm.add_widget(Builder.load_file("screen1.kv"))
        sm.add_widget(Builder.load_file("screen1.kv"))

        return sm

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

我也想通过按钮切换屏幕,但我不知道如何使用这些文件。

需要像其他脚本一样导入自定义 Screen 类:

main.py:

from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager,Screen
from sc1 import Screen1
from sc2 import Screen2
class MyApplication(MDApp):
    def build(self):
        Window.size = 300, 600
        sm = ScreenManager()
        sm.add_widget(Screen1())
        sm.add_widget(Screen2())
        return sm
if __name__ == "__main__":
    MyApplication().run()

sc1.py:

from kivy.uix.screenmanager import Screen
from kivy.lang.builder import Builder
class Screen1(Screen):
    Builder.load_file('screen1.kv')
    def buttFunc(self):
        print("Screen1")

sc2.py:

from kivy.uix.screenmanager import Screen
from kivy.lang.builder import Builder
class Screen2(Screen):
    Builder.load_file('screen2.kv')
    def buttFunc2(self):
        print("Screen2")

screen1.kv:

<Screen1>:
    name: "screen1"
    BoxLayout:
        Button:
            text: "Screen 1"
            on_release: root.buttFunc()

        Button:
            text: "Go To Screen2"
            on_release: app.get_running_app().root.current = 'screen2'

screen2.kv:

<Screen2>:
    name: "screen2"
    BoxLayout:
        Button:
            text: "Screen 2"
            on_release: root.buttFunc2()
        Button:
            text: "Go To Screen1"
            on_release: app.get_running_app().root.current = 'screen1'