Lax_Sam
Lax_Sam

Reputation: 1139

Only one root object is allowed by .kv

I am doing an online kivy tutorial. Below is the code that I have written. It is exactly as was in the tutorial video.

But when I run the code I get the following error:

 ...
     1:import FadeTransition kivy.uix.screenmanager.FadeTransition
     2:
 >>  3:ScreenManagement:
     4: transition: FadeTransition()
     5: 
 ...
     Only one root object is allowed by .kv

I think the error is with the FadeTransition because I have two instances-MainScreen and AnotherScreen but I do not know how to rectify it. And in the tutorial video the code ran properly. So why isn't it executing in my computer?

Below is the code that I have written

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


class MainScreen(Screen):
   pass

class AnotherScreen(Screen):
   pass

class ScreenManagement(ScreenManager):
   pass



presentation = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
   transition: FadeTransition()
   MainScreen:
   AnotherScreen:


<MainScreen>:
   name: "main"
   Button: 
       text: "Next Screen"
       font_size: 50
       on_release: root.app.current = "other"

<AnotherScreen>:
   name: "other"
   Button: 
       text: "Back Home"
       font_size: 50
       on_release: root.app.current = "main"

 ''')


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

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

Thanks in advance.

Upvotes: 0

Views: 5432

Answers (3)

Borish meitei
Borish meitei

Reputation: 21

Try this code it might help a bit took a long time for me to figure it out:

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


class MainScreen(Screen):
   pass

class AnotherScreen(Screen):
  pass

class ScreenManagement(ScreenManager):
  pass



presentation = ('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
   transition: FadeTransition()
   MainScreen:
   AnotherScreen:


<MainScreen>:
   name: "main"
   Button: 
      text: "Next Screen"
      font_size: 50
      on_release: root.app.current = "other"

<AnotherScreen>:
   name: "other"
   Button: 
      text: "Back Home"
      font_size: 50
      on_release: root.app.current = "main"

''')


class MainApp(App):
   def build(self):
      return Builder.load_string(presentation)

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

Upvotes: 2

ikolim
ikolim

Reputation: 16021

In your kv file, you need to surround ScreenManagement class with "<" and ">" as shown in the example below.

main.py

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


class ScreenManagement(ScreenManager):
    pass


class MainScreen(Screen):
    pass


class AnotherScreen(Screen):
    pass


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

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

main.kv

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

<ScreenManagement>:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    Button:
        text: "Go To Next Screen"
        color:0,0,0,1
        background_color: 1,1,1,1
        size_hint: 1,0.10
        pos:0,200
        font_size: 30
        on_release: app.root.current = "another"

<AnotherScreen>:
    name: "another"
    Button:
        text: "Go To Main Screen"
        color:0,0,0,1
        background_color: 1,1,1,1
        size_hint: 1,0.10
        pos:0,200
        font_size: 30
        on_release: app.root.current = "main"

Output

enter image description here

Upvotes: 1

user8553454
user8553454

Reputation:

I came across the exact issue as you while watching the same video. You will need to do this instead (assuming you want a fade transition):

Main.py file:

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

class ScreenManagement(ScreenManager): #here you are creating a screen manager called ScreenManagement
    pass
class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

presentation = Builder.load_file("Main2.kv") #telling the app which .kv file to use

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

runMain = MainApp()
runMain.run()

Now over to your Main2.kv file:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition() #telling the screen manager to use a fade transition
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main" #this name is what the screen manager uses to distinguish which screen to go to
    Button:
        text: "Go To Next Screen"
        color:0,0,0,1
        background_color: 1,1,1,1
        size_hint: 1,0.10
        pos:0,200
        font_size: 30
        on_release: app.root.current = "another" #the screen that the screen manager is told to go to

<AnotherScreen>:
    name: "another"
    Button:
        text: "Go To Main Screen"
        color:0,0,0,1
        background_color: 1,1,1,1
        size_hint: 1,0.10
        pos:0,200
        font_size: 30
        on_release: app.root.current = "main"

Hope that helped.

Upvotes: 1

Related Questions