location 属性 没有被添加到我在 kivy 程序中的小部件

location property is not getting added to my widget in kivy program

这是我的 Python 代码, 我正在从书中学习 kivy 'creating apps in kivy ' 但 'i tried to make a code myself ' 但我被困在这里 '

在此代码中,当我单击列表视图小部件上的任何按钮时,它显示错误:

过去几个小时我一直在努力解决这个问题...

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 显示 'MainWindow' 对象没有属性 'location'

因为您的位置只是一个临时变量。将其设为属性并调用 self.location