列表项 (Kivy & KivyMD) 循环值的回调
Posted
技术标签:
【中文标题】列表项 (Kivy & KivyMD) 循环值的回调【英文标题】:callback for loop values of List Item's (Kivy & KivyMD) 【发布时间】:2021-07-16 01:00:09 【问题描述】:这里我有一个循环,从 JSON 列表中查找键和值
目前,KivyMD 列表的视图是正确的,但我想将正确的键传递到打印上,而不是打印出 JSON 中的 last key。
ScrollView:
MDList:
id: container
def on_start(self):
for (self.k, v) in self.data.items():
item = TwoLineAvatarListItem(text=str(self.k),secondary_text=str(v["NAME"]),on_release=self.show_data)
image = ImageLeftWidget(source=v["Image"])
item.add_widget(image)
self.root.ids.container.add_widget(item)
def show_data(self,obj):
print(self.k)
值:总是打印 E101a
总结一下,我想单独查看“容器值”
【问题讨论】:
您的代码没有按原样运行。你在用什么库? 【参考方案1】:乔纳斯!我已经解决了这个问题:
on_release=lambda x, value_for_pass=value: self.passValue(value_for_pass)
在这样的循环中:
.py 文件:
# (...)
class AllClientsScreen(Screen):
def on_enter(self):
self.show_clients()
def show_clients(self):
for i in range(10):
client_id = i
name = "Roger" + str(client_id) + " id: " + str(client_id)
last_name = "Rabbit" + str(client_id)
phone = "555" + str(client_id)
email = "roger" + str(client_id) + "@rabbit.com"
item = ThreeLineListItem(
text = name + " " + last_name,
secondary_text = phone,
tertiary_text = email,
on_release=lambda x, value_for_pass=name: self.passValue(value_for_pass)
)
self.ids.container.add_widget(item)
def passValue(self, *args):
args_str = ','.join(map(str,args))
screen2 = self.manager.get_screen ('oneclient')
screen2.ids.client_id.text = args_str
self.manager.current = 'oneclient'
print(args)
.kv 文件:
#:import NoTransition kivy.uix.screenmanager.NoTransition
ScreenManager:
transition: NoTransition()
MenuScreen:
AllClientsScreen:
OneClientScreen:
<MenuScreen>:
name: 'menu'
MDRectangleFlatButton:
text: 'All Clients'
pos_hint: 'center_x': 0.5, 'center_y':0.5
on_release: root.manager.current = 'allclients'
<AllClientsScreen>:
name: 'allclients'
ScrollView:
MDList:
id: container
padding: dp(20)
MDLabel:
id: clientsdata
text: 'All Clients'
halign: 'center'
MDRectangleFlatButton:
text: 'Menu'
pos_hint: 'center_x': 0.5, 'center_y':0.4
on_release: root.manager.current = 'menu'
<OneClientScreen>:
name: 'oneclient'
MDLabel:
id: client_id
text: 'Cliente'
halign: 'center'
ScrollView:
MDList:
id: container
padding: dp(40)
MDRectangleFlatButton:
text: 'Menu'
pos_hint: 'center_x': 0.5, 'center_y':0.4
on_release: root.manager.current = 'menu'
我刚刚发布了代码的基本部分,如果您有任何疑问或成功,请告诉我。 祝你发展顺利!
【讨论】:
以上是关于列表项 (Kivy & KivyMD) 循环值的回调的主要内容,如果未能解决你的问题,请参考以下文章