如何从列表视图中的ListItemButton捕获文本值并将该文本值存储在ObjectProperty中,以便我可以在其他函数中使用ListItemButton文本值?
我收到错误:
AttributeError: 'NoneType' object has no attribute 'adapter'
参考文献: https://groups.google.com/forum/#!msg/kivy-users/4opq2wsZEKs/XM9f1Psj8JUJ
截图:
尝试在python中完成类似的事情:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.properties import *
from random import randint
import cx_Oracle
import os
class MyGrid(GridLayout):
data = []
team_list = ListProperty()
search_input = ObjectProperty()
cols = NumericProperty()
lv11 = ObjectProperty()
lv22 = ObjectProperty()
team1 = ObjectProperty()
team2 = ObjectProperty()
list_adapter = lv11.adapter
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.fetch_team_list()
self.list_adapter = self.lv11.adapter
self.list_adapter.bind(on_selection_change=self.respond_list_view_action())
def respond_list_view_action(self):
text = self.list_adapter.get_view(ListItemButton.text)
self.team1 = text
def args_converter(self, index, data_item):
return {'text': data_item['TEAM'],
'size_hint_y': None,
'height': 25}
def fetch_team_list(self):
con = cx_Oracle.connect('SCOTT/******@localhost/j1db')
cur = con.cursor()
statement = 'Select TEAM from fbsteampassing group by TEAM'
exe = cur.execute(statement)
columns = [i[0] for i in cur.description]
exe2 = [dict(zip(columns, row)) for row in cur]
self.team_list = []
for row in exe2:
self.team_list.append(row)
return self.team_list
...
相关.kv文件
#:kivy 1.9.0
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
#:import ListItemButton kivy.uix.listview.ListItemButton
TabbedPanel:
do_default_tab: False
tab_width: 200
TabbedPanelItem:
text: "Team Query"
BoxLayout:
orientation: "vertical"
TextInput:
id: search_box
focus: True
size_hint_y: .1
multiline: False
on_text_validate: rootgrid.on_enter()
Button:
size_hint_y: .1
text: "Return"
on_press: rootgrid.on_enter()
ScrollView:
size_hint_y: None
height: '500dp'
MyGrid:
id: rootgrid
search_input: search_box
lv11: lv1
cols: 10
size_hint_y: None
height: self.minimum_height
spacing: '1dp'
TabbedPanelItem:
text: "Select All"
BoxLayout:
orientation: "vertical"
Button:
size_hint_y: .1
text: "Return All Data"
on_press: rootgrid2.on_enter_2()
ScrollView:
size_hint_y: None
height: '500dp'
MyGrid:
id: rootgrid2
search_input: search_box
cols: 10
size_hint_y: None
height: self.minimum_height
spacing: '1dp'
TabbedPanelItem:
text: "Compare Teams"
StackLayout:
ScrollView:
size_hint_x: .5
size_hint_y: None
height: '100dp'
ListView:
id: lv1
adapter:
ListAdapter(data=rootgrid.team_list, cls=ListItemButton, args_converter=rootgrid.args_converter)
ScrollView:
lv1: lv1
size_hint_x: .5
size_hint_y: None
height: '100dp'
ListView:
id: lv2
adapter:
ListAdapter(data=rootgrid.team_list, cls=ListItemButton, args_converter=rootgrid.args_converter)
Button:
size_hint: (1, .1)
text: "Return Team Compare"
on_press: rootgrid3.on_enter_3()
ScrollView:
size_hint_y: None
height: '500dp'
MyGrid:
id: rootgrid3
search_input: search_box
cols: self.cols
size_hint_y: None
height: self.minimum_height
spacing: '1dp'
...
答案 0 :(得分:0)
直接回答:您可以从绑定到ListAdapter的on_selection_change
link事件的函数中捕获值。名称字符串将存储在adapter.selection[0].text
属性中。你可以从那里做更多的计算。
你的代码有点长,凌乱,我不知道错误来自哪一行。我有一个例子:
test.kv:
#:kivy 1.9.0
Screen:
GridLayout:
cols: 1
TeamsGrid:
ScoreGrid:
<TeamsGrid@GridLayout>:
rows: 1
padding: '50dp', '20dp'
spacing: '10dp'
TeamsView:
name: 'left'
TeamsView:
name: 'right'
<ScoreGrid>:
cols: 2
TeamName:
id: left_name
TeamName:
id: right_name
TeamScore:
id: left_score
bgcolor: 0.1, 0.1, 0.3
TeamScore:
id: right_score
bgcolor: 0.3, 0.1, 0.1
<TeamName@Label>:
size_hint_y: 0.1
text: 'score'
<TeamScore@Label>:
markup: True
bgcolor: 0, 0, 0
canvas.before:
Color:
rgb: self.bgcolor
Rectangle:
pos: self.pos
size: self.size
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.uix.listview import ListView, ListItemButton
from kivy.app import App
from kivy.adapters.listadapter import ListAdapter
from kivy.properties import StringProperty
from kivy.uix.gridlayout import GridLayout
from kivy.clock import mainthread
beans = {}
class TeamsView(ListView):
name = StringProperty()
def __init__(self, **kwargs):
super(TeamsView, self).__init__(**kwargs)
self.fetch_teams_from_database()
self.set_adapter()
self.save_bean()
def fetch_teams_from_database(self):
self.teams = [
{'team': 'Pogoń Szczecin', 'score': '55'},
{'team': 'Arizona State', 'score': '32'},
{'team': 'Idaho', 'score': '44'},
{'team': 'Nebraska', 'score': '48'},
{'team': 'dummy', 'score': '0'},
{'team': 'dummy', 'score': '0'},
{'team': 'dummy', 'score': '0'},
{'team': 'dummy', 'score': '0'},
{'team': 'dummy', 'score': '0'}
]
def set_adapter(self):
self.adapter = ListAdapter(
data=self.teams,
args_converter=self.args_converter,
selection_mode='single',
allow_empty_selection=True,
cls=ListItemButton
)
self.adapter.bind(
on_selection_change=self.on_selection_change)
def args_converter(self, row_index, item):
return {
'text': item['team'],
'size_hint_y': None,
'height': 50
}
def on_selection_change(self, adapter):
team_score, team_name = self.get_team(adapter)
team_score = '[size=50]' + team_score + '[/size]'
beans['score_grid'].ids[self.name + '_name'].text =\
team_name + ' score:'
beans['score_grid'].ids[self.name + '_score'].text =\
team_score
def get_team(self, adapter):
try:
for team in self.teams:
if team['team'] in adapter.selection[0].text:
return team['score'], team['team']
except IndexError:
return '', ''
@mainthread
def save_bean(self):
beans[self.name + 'list'] = self.proxy_ref
class ScoreGrid(GridLayout):
def __init__(self, **kwargs):
super(ScoreGrid, self).__init__(**kwargs)
beans['score_grid'] = self.proxy_ref
class Test(App):
pass
Test().run()