python - 从 listitembutton 捕获值到 textinput kivy/python
问题描述
我创建了一个移动应用程序。一旦用户单击列表中的 1 个元素,我需要将文本输入另一个文本输入
我的kv文件
#:import main main
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
#:import ListItemButton kivy.uix.listview.ListItemButton
<LocationButton>:
deselected_color:0.4, 1, 1,1
selected_color: 0, 0, 1, 1
size: (100, '48dp')
on_press:root.test()
<ecran1>:
nom_du_produit:le_produit
FloatLayout:
ListView:
id:liste_des_produits
size_hint:.5,.8
pos_hint:{'x':.25,'y':.0}
adapter:
ListAdapter(data=root.L,cls=main.LocationButton)
TextInput:
id:le_produit
text:''
font_size:20
size_hint:.2,.1
pos_hint:{'x':.78,'y':.75}
background_color:1,1,1,1
multiline:False
我的python代码
import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen,ScreenManager,WipeTransition
from kivy.properties import ObjectProperty,StringProperty,ListProperty
from kivy.uix.listview import ListItemButton
class ecran1(Screen):
L=ListProperty(['tomate','abricot'])
nom_du_produit = ObjectProperty()
class LocationButton(ListItemButton):
L = ListProperty(['tomate', 'abricot'])
nom_du_produit = ObjectProperty()
def test(self):
selection_1 = self.liste.adapter.selection[0].text
self.nom_du_produit.text = selection_1
class PongApp(App):
def build(self):
return ecran1()
if __name__ == '__main__':
PongApp().run()
我尝试了几个选项,但答案总是属性错误
更好理解的图片:这张图片
解决方案
至少有两种不同的方式来完成你想要的。这是您的test
方法的修改,显示了两者:
def test(self):
#App.get_running_app().root.nom_du_produit.text = self.text # uses the ObjectProperty in the ecran1 class
App.get_running_app().root.ids.le_produit.text = self.text # uses the le_produit id in the TextInput class
推荐阅读
- javascript - 如何在特定时间继续单个 Redux Actions
- css - 如何在 Angular 中的 Mat 选择中测试 Input 和 mat 选项
- c# - 显示属性名称给出资源找不到错误
- javascript - js中改变字体大小的函数。如何添加缩放限制?
- php - PHP 7 的路径 - 使用“|” 特点
- mysql - MySQL 查询 - 显示更高的“薪水”
- python - 带有二维索引的 numpy 多维选择
- php - CakePHP 3.6.14:在引导模式中呈现视图 (.ctp)
- android - 如何轻松访问在按钮单击时膨胀的片段中的视图?
- javascript - 如何从 Google API 检索用户名