在Kivy的另一个内部返回一个BoxLayout?

时间:2017-01-02 15:00:46

标签: python-3.x kivy

我试图在Python 3.5和kivy中创建一个简单的应用程序,从一个简单的屏幕开始,当你点击它时,转到另一个显示3个列表,让你选择数据:

Python文件:

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

kivy文件:

#: 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)

当我尝试运行应用程序时,它会告诉我:&#34;未知类&#34; 这很奇怪,因为Ex44类是单独工作的,但是当我试图将它添加到主应用程序逻辑中时却没有。 我试图为该类返回一个小部件而不是BoxLayout,在kivy文件中单独返回Ex44,但我总是得到相同的错误。

是否可以在Kivy中将另一个BoxLayout返回到其中?

1 个答案:

答案 0 :(得分:1)

您正在构建kv文件太快(在定义类之前)。将 Builder.from_file 调用移动到构建方法

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