class MyScreen(Screen): data_items = ListProperty([])
def __init__(self, **kwargs): super(MyScreen, self).__init__(**kwargs) self.get_data() def get_data(self): connection = sqlite3.connect('test.db') cursor = connection.cursor() cursor.execute("SELECT * FROM table ORDER BY pos ASC") rows = cursor.fetchall() for row in rows: for col in row: self.data_items.append(col)
<MyScreen>: BoxLayout: orientation: "vertical"
GridLayout: size_hint: 1, None size_hint_y: None height: 25 cols: 6
Label: text: "Pos" Label: text: "Name" Label: text: "A" Label: text: "B" Label: text: "C" Label: text: "Num"
BoxLayout: data: [{'text': str(x)} for x in root.data_items] cols: 6 default_size: None, dp(26) default_size_hint: 1, None size_hint_y: None height: self.minimum_height orientation: 'vertical' multiselect: True touch_multiselect: True
Start by creating a minimal runnable example.
If you have a small number of items that can fit in a Window, use a Grid or BoxLayout
If you have up to say 100 line items use a ScrollView.
If you have more or plan on primarily running on mobile, use a RecycleView.
Give it a try.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6f65c760-f4ce-43c5-97a5-7f2bb7db0314o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
Create a class that holds one row of data.
For each row of data, instance the class and initialize with the data.
Add the class to the widget tree under a scrollview.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6bdd07ea-42e9-4eb7-b904-7b184073c4b1o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6bdd07ea-42e9-4eb7-b904-7b184073c4b1o%40googlegroups.com.
Here is an example.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
kv = """
<DataLine>: # class to hold one line of data
canvas:
Color:
rgb: .3, .3, .3
Line:
width: 2
rectangle: (*self.pos, *self.size)
size_hint_y: None
height: 48
Label:
text: root.name
Label:
text: root.color
Label:
text: root.birthday
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: None
height: 48
text: 'Data List in a Scrollview Example'
BoxLayout: # Headings for
size_hint_y: None
height: 48
Label:
text: 'Name'
Label:
text: 'Favorite Color'
Label:
text: 'Birthday'
ScrollView:
do_scroll_y: True
bar_width: dp(10)
scroll_type: ['bars','content']
BoxLayout:
orientation: 'vertical'
id:scroll_box
size_hint_y: None
height: self.minimum_height
"""
class DataLine(BoxLayout): # class to hold one line of data
name = StringProperty()
color = StringProperty()
birthday = StringProperty()
class DataApp(App):
def build(self):
return Builder.load_string(kv)
def on_start(self):
info = [['Michael', 'Blue', '1/30/2020'], # simulating what could come out of a database
['Carol', 'Green', '12/3/2020'],
['Bill', 'Red', '3/12/2020'],
['Sue', 'Orange', '7/4/2020'],
['John', 'Blue', '5/23/2020'],
['Jane', 'Yellow', '3/9/2020'],
['Michael', 'Blue', '2/30/2020'],
['Carol', 'Green', '4/3/2020'],
['Bill', 'Red', '3/12/2020'],
['Sue', 'Orange', '8/4/2020'],
['John', 'Blue', '10/23/2020'],
['Jane', 'Yellow', '6/9/2020']]
for line in info:
name, color, bd = line
w = DataLine(name=name, color=color, birthday=bd) # instancing the class
self.root.ids.scroll_box.add_widget(w) # adding the class to the scrollview
DataApp().run()
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fa224291-abf8-4cfc-aa0b-61e9403bda69o%40googlegroups.com.
from kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenimport sqlite3from kivymd.app import MDAppfrom kivy.properties import ListProperty, StringPropertyfrom kivy.uix.boxlayout import BoxLayout
kv = '''
BoxLayout: orientation: 'vertical' MDToolbar: id: toolbar elevation: 10 title: "Sports App" left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationLayout: ScreenManager: id: screen_manager HomeScreen: id: home name: 'home' FBScreen: id: fb name: 'fb' MDNavigationDrawer: id: nav_drawer ScrollView: MDList: OneLineListItem: icon: 'soccer' text: "Home" on_press: nav_drawer.set_state("close") screen_manager.current = "home" OneLineListItem: text: "Football" on_press: nav_drawer.set_state("close") screen_manager.current = "fb"
<HomeScreen>: MDLabel: text: 'Home screen' halign: 'center'
<FBData>:
canvas: Color: rgb: .3, .3, .3 Line: width: 2 rectangle: (*self.pos, *self.size) size_hint_y: None height: 48 Label:
text: root.fbPos Label: text: root.fbName Label: text: root.fbW Label: text: root.fbL Label: text: root.fbD Label: text: root.fbP
<FBScreen>:
BoxLayout: orientation: 'vertical' Label: size_hint_y: None height: 48 text: 'Data List in a Scrollview Example' BoxLayout: # Headings for size_hint_y: None height: 48 Label:
text: 'Pos' Label: text: 'Name' Label: text: 'W' Label: text: 'L' Label: text: 'D' Label: text: 'Point'
ScrollView: do_scroll_y: True bar_width: dp(10) scroll_type: ['bars','content'] BoxLayout: orientation: 'vertical' id:scroll_box size_hint_y: None height: self.minimum_height
'''
class FBData(BoxLayout): fbName = StringProperty() fbPos = StringProperty() fbW = StringProperty() fbL = StringProperty() fbD = StringProperty() fbP = StringProperty()
class FBScreen(Screen): def on_start(self): con = sqlite3.connect('test.db') cur = con.cursor() cur.execute("SELECT * FROM soccer ORDER BY pos ASC") table = cur.fetchall()
for line in table: pos, name, win, lose, draw, point = line w = FBData(fbPos = str(pos), fbName = name, fbW = str(win), fbL = str(lose), fbD = str(draw), fbP = str(point)) self.ids.scroll_box.add_widget(w)
def __init__(self, **kwargs): super(FBScreen, self).__init__(**kwargs) self.on_start() class ScrManage(ScreenManager): pass
class Sports(MDApp): def build(self): return Builder.load_string(kv)
Sports().run()
AttributeError: 'super' object has no attribute '__getattr__'
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fa224291-abf8-4cfc-aa0b-61e9403bda69o%40googlegroups.com.
You do not want to use ids in the __init__() the kv code has not been processed yet. Instead you can use on_kv_post()
See: https://kivy.org/doc/stable/api-kivy.uix.widget.html?highlight=widget#kivy.uix.widget.Widget
class FBScreen(Screen):
def on_kv_post(self, base_widget):
con = sqlite3.connect('test.db')
cur = con.cursor()
cur.execute("SELECT * FROM soccer ORDER BY pos ASC")
table = cur.fetchall()
for line in table:
pos, name, win, lose, draw, point = line
w = FBData(
fbPos=str(pos), fbName=name, fbW=str(win), fbL=str(lose), fbD=str(draw), fbP=str(point))
self.ids.scroll_box.add_widget(w)
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c6cf190-32e3-469b-833a-6c7ed2729c80o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c6cf190-32e3-469b-833a-6c7ed2729c80o%40googlegroups.com.
😊
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1fde09f1-57cf-4969-b885-f640f8ffe678o%40googlegroups.com.