0

I am trying to add navigation in my kivy app using .py file but because the things I tried are not working and I am a complete beginner. I don't know what problem is occurring and I am not able to find any solutions on my own pls help me. I am trying to change the main screen from class Dre to class SecondWindow when the play button is clicked. BTW the things I already tried are tagged now. Pls give the solution in .py file because I am trying to do everything on it. Here is the code. Thanks

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

ash2021
  • 63
  • 5

1 Answers1

0

Here's the correct code for that (at least for me):

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

The main changes here are the fact that the build method for LabApp is required, since the .kv file isn't used; to also inherit the Screen class into the MainWindow class, which is renamed from Dre for less confusing code, since it is essential forScreenManager; and the bruh method in the MainWindow (im not good at naming ok dont roast me) that changes to the SecondWindow screen when the B1 button's on_press event is called. Sorry for the confusing English but at least it's understandable(?) ig

Weebify
  • 561
  • 4
  • 13
  • Thanks, mate, it's working:)) I didn't know how to continue with my project without solving this problem, but ya helped me to:), I will now just write some code to read and write files in it so the navigation works accordingly to the text file. BTW I am also a high-school self-learner more like a middle-school self-learner. :) – ash2021 Aug 29 '21 at 02:32
  • Also can you please explain what is `lambda x:` in `on_press= lambda x: self.bruh()` doing? I mean what's the purpose of `lambda x:` and when can I use that. Thanks in advanced:) – ash2021 Aug 29 '21 at 17:24
  • About that one, I don't really know what's wrong since I'm also sort of new to the kivy scene (just started learning a month ago haha). Before creating the lambda function I tried with `self.bruh` and `self.bruh()`, which gave `TypeError: bruh() takes 1 positional argument but 2 were given` and `AssertionError: None is not callable` errors respectively. About the first one, you can check this question: https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given and I believe there's nothing you can do about it. – Weebify Aug 31 '21 at 08:22
  • And the second one, I have no idea what's wrong there but what I know is that it doesn't work :-;. And finally about when you can use the `lambda` function, I would say that it's your last resort if the other 2 doesn't work, like in this example I showed you. I'm quite new to Python too so I can't do much, but hope this helps :) – Weebify Aug 31 '21 at 08:24