这是我的Python代码,我正在从《在kivy中创建应用程序》一书中学习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显示“ MainWindow”对象没有属性“位置”
因为您的位置只是一个临时变量。使其成为属性并调用self.location
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句