我有2个班级(一个班级包含会议列表,另一个班级包含该特定会议中的出席者列表)。
我创建了第一个列表视图,该列表视图显示了会议列表。
我想基于对第一个列表视图(会议)的点击来提供第二个列表视图(与会者)。
目前,我无法调用find_attendant_meeting()。
当我使用:
调用此函数时1)
<MeetingListButton>:
on_press: root.find_attendant_meeting()
我无权访问{attendance_list}对象。
class MeetingListButton(ListItemButton):
def find_attendant_meeting(self):
selected_meeting = int(self.proxy_ref.text[-1])
sql_attendance_meeting = cur.execute("""SELECT name FROM users WHERE id IN (SELECT user_meeting.id_user from meeting join user_meeting on user_meeting.id_meeting = meeting.id where meeting.id='%s')""" % selected_meeting)
res = ['Attendant Name: ' + str(t[0]) for t in sql_attendance_meeting]
print(res)
return res
class AttendanceListButton(ListItemButton):
pass
class MeetingDB(BoxLayout):
meeting_list = ObjectProperty()
attendance_list = ObjectProperty()
class FaceApp(App):
r = ['Meeting Number ' + str(t[0]) for t in sql_meeting_list]
def build(self):
return MeetingDB()
KV
文件:
<MeetingDB>:
orientation:'vertical'
meeting_list: meeting_view
attendance_list: attendance_view
ListView:
id: meeting_view
adapter:
ListAdapter(data = app.r, cls = main.MeetingListButton)
ListView:
id: attendance_view
adapter:
ListAdapter(data = [], cls = main.AttendanceListButton)
<MeetingListButton>:
on_press: root.find_attendant_meeting()
答案 0 :(得分:0)
一种简单的方法是在App.get_running_app
方法内使用find_attendant_meeting
方法,以ListView
为id
来访问attendance_view
小部件。
def find_attendant_meeting(self):
selected_meeting = int(self.proxy_ref.text[-1])
sql_attendance_meeting = cur.execute("""SELECT name FROM users WHERE id IN (SELECT user_meeting.id_user from meeting join user_meeting on user_meeting.id_meeting = meeting.id where meeting.id='%s')""" % selected_meeting)
res = ['Attendant Name: ' + str(t[0]) for t in sql_attendance_meeting]
App.get_running_app().root.ids.attendance_view.adapter.data = res
由于正在查询数据库,因此您可能希望在另一个线程上进行查询,以使gui不会冻结。
另一种方法是在MessageListButton
小部件上创建一个对象。
class MeetingListButton(ListItemButton):
someRoot = ObjectProperty()
然后您可以在kv文件中设置该对象的值,如下所示:
<MeetingListButton>:
someRoot: app.root
on_press: root.find_attendant_meeting()
然后在您的find_attendant_meeting
方法上,您可以执行以下操作:
def find_attendant_meeting(self):
selected_meeting = int(self.proxy_ref.text[-1])
#sql_attendance_meeting = cur.execute("""SELECT name FROM users WHERE id IN (SELECT user_meeting.id_user from meeting join user_meeting on user_meeting.id_meeting = meeting.id where meeting.id='%s')""" % selected_meeting)
#res = ['Attendant Name: ' + str(t[0]) for t in sql_attendance_meeting]
dummyData = {0: ["Aaa", "Bbb", "Ccc"], 1: ["Ddd", "Eee", "Fff"], 2: ["Ggg", "Hhh", "Iii"]}
sql_attendance_meeting = dummyData[selected_meeting]
self.someRoot.ids.attendance_view.adapter.data = sql_attendance_meeting
这是完整的代码:
main.py
from kivy.adapters.listadapter import ListAdapter
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListView, ListItemButton
from kivy.properties import ObjectProperty
class MeetingListButton(ListItemButton):
someRoot = ObjectProperty()
def find_attendant_meeting(self):
selected_meeting = int(self.proxy_ref.text[-1])
#sql_attendance_meeting = cur.execute("""SELECT name FROM users WHERE id IN (SELECT user_meeting.id_user from meeting join user_meeting on user_meeting.id_meeting = meeting.id where meeting.id='%s')""" % selected_meeting)
#res = ['Attendant Name: ' + str(t[0]) for t in sql_attendance_meeting]
dummyData = {0: ["Aaa", "Bbb", "Ccc"], 1: ["Ddd", "Eee", "Fff"], 2: ["Ggg", "Hhh", "Iii"]}
sql_attendance_meeting = dummyData[selected_meeting]
self.someRoot.ids.attendance_view.adapter.data = sql_attendance_meeting
class AttendanceListButton(ListItemButton):
pass
class MeetingDB(BoxLayout):
meeting_list = ObjectProperty()
attendance_list = ObjectProperty()
class FaceApp(App):
r = ["Meeting Number " + str(t) for t in range(3)]
def build(self):
return MeetingDB()
FaceApp().run()
face.kv
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
#:import main main
<MeetingDB>:
orientation:'vertical'
meeting_list: meeting_view
attendance_list: attendance_view
ListView:
id: meeting_view
adapter: ListAdapter(data = app.r, cls = main.MeetingListButton)
ListView:
id: attendance_view
adapter: ListAdapter(data = [], cls = main.AttendanceListButton)
<MeetingListButton>:
someRoot: app.root
on_press: root.find_attendant_meeting()