2015-02-10 20 views
0

這是我的Python代碼, 我正在學習kivy從書'創建應用程序在基維',但'我試圖自己做一個代碼',但我卡在這裏'位置屬性沒有得到添加到我的部件在基維程序

在此代碼當我點擊列表視圖控件的任何按鈕

它顯示了一個錯誤:

我試圖解決它過去的幾個小時...

from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.listview import ListItemButton 
from kivy.properties import ListProperty,ObjectProperty 
from kivy.factory import Factory 
class ListButton(ListItemButton): 
    # List Button Must Have location property been set to List Property so that 
    # values returned by args_converter must get stored to it 
    location = ListProperty() 

class Weather2App(App): 
    pass 

class MainWindow(BoxLayout): 

    def this(self): 
     # we have have created a list here with strings in it 
     cities = [('city','town2'),('city2','town3')] 

     # here we have assigned this list to item_strigs property of the 
     # list item button , which is data 
     self.list_item.item_strings = cities 

     # using this we are forcing the object to show data on screen 
     self.list_item._trigger_reset_populate() 
     # this args conerter takes two argument , it is converting 
     # data into a dictionary 
    def args_converter(self,index,data_item): 
     city,town = data_item 
     return {'location':(city,town)} 

class MainCity(BoxLayout): 
    pass 

class MainWindow1(BoxLayout): 
    this_wid = ObjectProperty() 
    def show_city(self,location=None): 
     self.clear_widgets() 
     if location is None and self.this_wid is Nonw: 
      self.this_wid.location = ['NEW','yORK'] 
     if location is not None: 
      self.this_wid = Factory.MainCity() 
      self.this_wid.location = location 
     self.add_widget(self.this_wid) 




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

Kivy代碼:

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


<ListButton>: 
    text:'{} ({})' .format(self.location[0],self.location[1]) 
    on_press:app.root.show_city(self.location) 
<MainWindow>: 
    list_item:_list_item 
    Button: 
     text:'this is a button' 
     on_press:root.this() 
    ListView: 
     id:_list_item 
     # here adapter is a kivy property ,but it s value is python , 
     # in which wea are constructing a kivy ListAdapter object 

     adapter: 
      ListAdapter(data=[],cls=main2.ListButton,args_converter=root.args_converter) 
MainWindow1: 
    MainWindow: 

<MainCity>: 
    Label: 

     text:'{} ({})' .format(root.location[0],root.location[1]) 

錯誤:

II是顯示出「主窗口」對象有沒有屬性「位置」

回答

0

因爲你的位置僅僅是一個臨時變量。將其設爲屬性並致電self.location

相關問題