在 table Kivy 中显示元组
Show tuple in a table Kivy
我有这个代码:
import pymysql
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
db = pymysql.connect("host", "user", "password", "database")
cursor = db.cursor()
cursor.execute("SELECT phone_info FROM ants WHERE id='onexT1'")
data_list = cursor.fetchall()
hello = list(data_list)
class ViewButton(Button):
def print_data(self, data):
print(data)
KV = '''
<ViewButton>:
on_release:
root.print_data(self.data)
RecycleView:
data: ()
viewclass: 'ViewButton'
RecycleBoxLayout:
default_size_hint: 1, None
orientation: 'vertical'
'''
class Test(App):
def build(self):
root = Builder.load_string(KV)
root.data = (item for item in hello)
return root
if __name__ == '__main__':
Test().run()
基本上,数据库中的查询结果存储为元组。但是当我 运行 它时,它 returns 一个错误:
AttributeError: 'tuple' object has no attribute 'get'
所以我尝试将元组转换为列表,但它 returns 出现与上述相同的错误。
我想要的是使用 recycleview 在 table 中显示 tuple/list 的内容。谢谢:)
RecycleView 需要哈希表的可迭代对象,例如字典列表,其中字典的键是将在视图类中使用的属性,这在 docs 中指示:
data:
The data used by the current view adapter. This is a list of dicts
whose keys map to the corresponding property names of the viewclass.
data is an AliasProperty that gets and sets the data used to generate
the views.
fetchall returns 元组列表,我们必须将该元组列表转换为字典列表,其中字典键为 "text" 因为它是 属性 由 ViewButton 使用。
另一方面,ViewButton 没有数据 属性,只有文本,因此您必须保存该信息。
...
data_list = cursor.fetchall()
hello = [({"text": result[0]}) for result in data_list] # convert
class ViewButton(Button):
def print_data(self, data):
print(data)
KV = '''
<ViewButton>:
on_release:
root.print_data(self.text) # <---
RecycleView:
viewclass: 'ViewButton'
RecycleBoxLayout:
default_size_hint: 1, None
orientation: 'vertical'
'''
class Test(App):
def build(self):
root = Builder.load_string(KV)
root.data = hello
return root
if __name__ == '__main__':
Test().run()
我有这个代码:
import pymysql
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
db = pymysql.connect("host", "user", "password", "database")
cursor = db.cursor()
cursor.execute("SELECT phone_info FROM ants WHERE id='onexT1'")
data_list = cursor.fetchall()
hello = list(data_list)
class ViewButton(Button):
def print_data(self, data):
print(data)
KV = '''
<ViewButton>:
on_release:
root.print_data(self.data)
RecycleView:
data: ()
viewclass: 'ViewButton'
RecycleBoxLayout:
default_size_hint: 1, None
orientation: 'vertical'
'''
class Test(App):
def build(self):
root = Builder.load_string(KV)
root.data = (item for item in hello)
return root
if __name__ == '__main__':
Test().run()
基本上,数据库中的查询结果存储为元组。但是当我 运行 它时,它 returns 一个错误:
AttributeError: 'tuple' object has no attribute 'get'
所以我尝试将元组转换为列表,但它 returns 出现与上述相同的错误。
我想要的是使用 recycleview 在 table 中显示 tuple/list 的内容。谢谢:)
RecycleView 需要哈希表的可迭代对象,例如字典列表,其中字典的键是将在视图类中使用的属性,这在 docs 中指示:
data:
The data used by the current view adapter. This is a list of dicts whose keys map to the corresponding property names of the viewclass.
data is an AliasProperty that gets and sets the data used to generate the views.
fetchall returns 元组列表,我们必须将该元组列表转换为字典列表,其中字典键为 "text" 因为它是 属性 由 ViewButton 使用。
另一方面,ViewButton 没有数据 属性,只有文本,因此您必须保存该信息。
...
data_list = cursor.fetchall()
hello = [({"text": result[0]}) for result in data_list] # convert
class ViewButton(Button):
def print_data(self, data):
print(data)
KV = '''
<ViewButton>:
on_release:
root.print_data(self.text) # <---
RecycleView:
viewclass: 'ViewButton'
RecycleBoxLayout:
default_size_hint: 1, None
orientation: 'vertical'
'''
class Test(App):
def build(self):
root = Builder.load_string(KV)
root.data = hello
return root
if __name__ == '__main__':
Test().run()