LeBruceWayne
LeBruceWayne

Reputation: 109

return a BoxLayout inside another one in Kivy?

I'm trying to make a simple application in Python 3.5 and kivy that starts with a simple screen and when you click on it, goes to another one which shows 3 lists that let you chose the data:

The Python file:

from kivy.app import App

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

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from kivy.properties import ListProperty
from dataTopy import rlists

# Transition des ecrans:
class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("ex44.kv")

# 
class FirstListItemButton(ListItemButton):
    pass

class SecondListItemButton(ListItemButton):
    pass

class ThirdListItemButton(ListItemButton):
    pass


class Ex44(BoxLayout):
    d1 = ListProperty([str(i) for i in range(1990,2014)] )
    d2 = ListProperty(['']*100)
    d3 = ListProperty(['']*100)
    def change(self,c):
        try: self.d2,self.d3 = rlists(int(c.text))
        except:
            import os
            CurDir = os.getcwd()
            print('Can not find data in ' + CurDir) 
    def change1(self,c):
        print('M => '+c.text)
    def change2(self,c):
        print('F => '+c.text)

class Ex44App(App):

    def build(self):
        return presentation

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

The kivy file:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ex44 ex44

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

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

<AnotherScreen>:
    name: "other"
    BoxLayout:
        Ex44
        Button:
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            text: "Back Home"
            on_release: app.root.current = "main"
            pos_hint: {"right":1, "top":1}

<FirstListItemButton>:
    on_press: app.root.change(*args)

<SecondListItemButton>:
    on_press: app.root.change1(*args)

<ThirdListItemButton>:
    on_press: app.root.change2(*args)


<Ex44>:
    ListView:
        adapter:
            ListAdapter(data=root.d1,
            selection_mode='single',
            cls=ex44.FirstListItemButton)
    ListView:
        adapter:
            ListAdapter(data=root.d2,
            selection_mode='single',
            cls=ex44.SecondListItemButton)
    ListView:
        adapter:
            ListAdapter(data=root.d3,
            selection_mode='single',
            cls=ex44.ThirdListItemButton)

When I try to run the app, it tells me: "Unknown class " It is weird because the class Ex44 works alone but not when I'm trying to add it into the main application logic. I've tried to return a widget instead of a BoxLayout for the class, to return Ex44 alone in the kivy file, etc. but I always get the same error in return.

Is it possible to return a BoxLayout inside another one in Kivy?

Upvotes: 2

Views: 305

Answers (1)

Yoav Glazner
Yoav Glazner

Reputation: 8041

You are building the kv file too soon (before the class is defined). move the Builder.from_file call to the build method

...
def build(self):
    return Builder.load_file("ex44.kv")

Upvotes: 1

Related Questions