如何使用 .py 文件在 kivy 中添加导航?

How to add navigation in kivy using .py file?

我正在尝试使用 .py 文件在我的 kivy 应用程序中添加导航,但因为我尝试过的东西不起作用,而且我是一个完全的初学者。我不知道发生了什么问题,我无法自己找到任何解决方案请帮助我。单击播放按钮时,我试图将主屏幕从 class Dre 更改为 class SecondWindow。顺便说一句,我已经尝试过的东西现在都被标记了。请在 .py 文件中给出解决方案,因为我正在尝试对其进行所有操作。这是代码。谢谢

import kivy
from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle
from kivy import platform
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class MainWindow(Screen):
    pass



#class ThirdWindow(ScreenManager):
#    def load(self):
#        sm = ScreenManager
#        sm.add_widget(Dre(name='Dre'))
#        sm.add_widget(SecondWindow(name='SecondWindow'))
#        self.sm.current = 'Dre'

class Dre(RelativeLayout):
    def __init__(self, **kwargs):
        super(Dre, self).__init__(**kwargs)
        self.color = [254/255, 102/255, 37/255, 1]
        self.H_color = [254/255, 102/255, 37/255]
        self.sound_theme = None
        self.init_audio()
        # self.kv = Builder.load_file('Levels.py')
        # self.callback()

        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neo.png')

            self.bind(pos=self.update_bg)
            self.bind(size=self.update_bg)
        else:
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neon.png')

            self.bind(pos=self.update_bg)
            self.bind(size=self.update_bg)


    def update_bg(self, *args):
        if platform in ('linux', 'win', 'macosx'):
            self.bg.pos = self.pos
            self.bg.size = self.size
            self.add_widget(Label(text='D  R  E  A  M  S',
                          pos_hint={'center_x': .5, 'center_y': .8},
                          font_size='60dp', font_name='Roboto-Bold.ttf', color=self.H_color))
            B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15),
                     pos_hint={'center_x': .5, "center_y": .3}, background_color = self.color, background_normal='')
            # B1.bind(on_press=return self.kv)
            self.add_widget(B1)

        else:
            self.bg.pos = self.pos
            self.bg.size = self.size
            self.add_widget(Label(text='D  R  E  A  M  S',
                                  pos_hint={'center_x': .5, 'center_y': .8},
                                  font_size='30dp', font_name='Roboto-Bold.ttf', color=self.H_color))
            B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15),
                        pos_hint={'center_x': .5, "center_y": .3}, background_color=self.color, background_normal='',
                        on_press=self.callback)

            self.add_widget(B1)


   # def callback(self, instance):
   #     print('working')
   #     self.manager.current = 'SecondWindow'

    def init_audio(self):

        self.sound_theme = SoundLoader.load('Bg_theme.mp3')
        self.sound_theme.volume = 1
        self.sound_theme.loop = True
        if self.sound_theme:
            self.sound_theme.play()
            print('okay')
        else:
            print('not okay')


class SecondWindow(Screen):
    def __init__(self, **kwargs):
        super(SecondWindow, self).__init__(**kwargs)
        self.color = [254 / 255, 102 / 255, 37 / 255, 1]
        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, color=self.color)





class LabApp(App):
    def build(self):
        return Dre()


if __name__ == '__main__':
    LabApp().run()

这是正确的代码(至少对我而言):

import kivy
from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle
from kivy import platform
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class SM(ScreenManager):
    pass
    

class MainWindow(Screen, RelativeLayout):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.color = [254/255, 102/255, 37/255, 1]
        self.H_color = [254/255, 102/255, 37/255]
        self.sound_theme = None
        self.init_audio()
        # self.kv = Builder.load_file('Levels.py')
        # self.callback()

        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neo.png')

            self.bind(pos=self.update_bg)
            self.bind(size=self.update_bg)
        else:
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neon.png')

            self.bind(pos=self.update_bg)
            self.bind(size=self.update_bg)

    def bruh(self):
        App.get_running_app().root.current = 'SecondWindow'

    def update_bg(self, *args):
        if platform in ('linux', 'win', 'macosx'):
            self.bg.pos = self.pos
            self.bg.size = self.size
            self.add_widget(Label(text='D  R  E  A  M  S',
                          pos_hint={'center_x': .5, 'center_y': .8},
                          font_size='60dp', font_name='Roboto-Bold.ttf', color=self.H_color))
            B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15),
                     pos_hint={'center_x': .5, "center_y": .3}, background_color = self.color, background_normal='', on_press= lambda x: self.bruh())
            # B1.bind(on_press=return self.kv)
            self.add_widget(B1)

        else:
            self.bg.pos = self.pos
            self.bg.size = self.size
            self.add_widget(Label(text='D  R  E  A  M  S',
                                  pos_hint={'center_x': .5, 'center_y': .8},
                                  font_size='30dp', font_name='Roboto-Bold.ttf', color=self.H_color))
            B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15),
                        pos_hint={'center_x': .5, "center_y": .3}, background_color=self.color, background_normal='',
                        on_press=self.callback)

            self.add_widget(B1)


   # def callback(self, instance):
   #     print('working')
   #     self.manager.current = 'SecondWindow'

    def init_audio(self):

        self.sound_theme = SoundLoader.load('Bg_theme.mp3')
        self.sound_theme.volume = 1
        self.sound_theme.loop = True
        if self.sound_theme:
            self.sound_theme.play()
            print('okay')
        else:
            print('not okay')


class SecondWindow(Screen):
    def __init__(self, **kwargs):
        super(SecondWindow, self).__init__(**kwargs)
        self.color = [254 / 255, 102 / 255, 37 / 255, 1]
        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, color=self.color)


class LabApp(App):
    def build(self):
        sm = SM()
        sm.add_widget(MainWindow(name='MainWindow'))
        sm.add_widget(SecondWindow(name='SecondWindow'))
        return sm
    

if __name__ == '__main__':
    LabApp().run()

这里的主要变化是 LabAppbuild 方法是必需的,因为没有使用 .kv 文件;还将 Screen class 继承到 MainWindow class 中,它从 Dre 重命名以减少混淆代码,因为它对于 [=16= 是必不可少的];以及 MainWindow 中的 bruh 方法(我不擅长命名 ok don't roast me),当 B1 按钮的 on_press 事件时,该方法会更改为 SecondWindow 屏幕叫做。对于令人困惑的英语感到抱歉,但至少它是可以理解的(?)ig