'Kivy ScrollView in ScreenManager
I am trying to create several scrollable main screens that are connected with ScreenManager However, when I try, I get the following error:
Only one root object is allowed by .kv
This happens when I add the WindowsManager in the kv file.
Can anyone advice me with how to resolve this?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen
class WindowManager(ScreenManager):
pass
class MenuPage(Screen):
pass
class MainPage(Screen):
pass
class Sections(BoxLayout):
label_text = StringProperty()
kv = Builder.load_file('main text')
class Scrollable(App):
def build(self):
return kv
def on_start(self):
self.root.ids.sv_box.add_widget(Sections(label_text=''))
Scrollable().run()
**kv file**
WindowsManager:
MainPage:
MenuPage:
BoxLayout:
orientation: 'vertical'
ScrollView:
do_scroll_y: True
BoxLayout:
orientation: 'vertical'
id: sv_box
size_hint_y: None
height: self.minimum_height
<Sections>:
orientation: 'vertical'
size_hint_y: None
height: 800
BoxLayout:
size_hint: (1,.5)
Button:
text: 'Menu'
size_hint: (.3,1)
BoxLayout:
Label:
text: 'Time'
```
Solution 1:[1]
According to kivy documentation a .kv file must contain only one root widget at most. In other words you can not have more than one class in the left most indentation level (except for dynamic classes) in kvlang.
In your posted code of .kv file there are two classes (WindowsManager and BoxLayout) at left(most) with same indentation level.
As from your code it's not clear which one of these two is the root widget, I assumed WindowsManager to be the root and moved the BoxLayout stuff under MainPage. Thus your modified main text.kv file now looks like,
WindowManager: # WindowsManager
MainPage:
BoxLayout:
orientation: 'vertical'
ScrollView:
do_scroll_y: True
BoxLayout:
orientation: 'vertical'
id: sv_box
size_hint_y: None
height: self.minimum_height
MenuPage:
<Sections>:
orientation: 'vertical'
size_hint_y: None
height: 800
BoxLayout:
size_hint: (1,.5)
Button:
text: 'Menu'
size_hint: (.3,1)
BoxLayout:
Label:
text: 'Time'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ApuCoder |
