2017-01-02 42 views
2

我試圖做一個簡單的應用程序在Python 3.5和kivy,以一個簡單的屏幕開始,當你點擊它,去另一個顯示3個列表,讓你選擇的數據:在基維的另一個裏面還有一個BoxLayout?

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) 

當我嘗試運行應用程序,它告訴我:「未知類」 這是奇怪的,因爲該類Ex44獨立工作,但不是我試圖添加它進入主要的應用邏輯。 我試過爲類返回一個小部件,而不是BoxLayout,單獨在kivy文件中返回Ex44等,但我總是得到相同的錯誤回報。

是否有可能在Kivy中的另一個BoxLayout中返回?

回答

1

您正在構建kv文件太快(在定義類之前)。移動Builder.from_file調用構建方法

... 
def build(self): 
    return Builder.load_file("ex44.kv") 
相關問題