KivyMD - Listing Card Items

622 views
Skip to first unread message

kivy

unread,
Jan 26, 2021, 2:59:36 PM1/26/21
to Kivy users support
Hello, I want to list card items from database. But every card item is over and over.

And how can I put title and body text from database?

Here is my code:

<NScreen>:
    
    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: None, None
        size: "280dp", "180dp"
        pos_hint: {"center_x": .5, "center_y": .5}

        MDLabel:
            text: "Title"
            theme_text_color: "Secondary"
            size_hint_y: None
            height: self.texture_size[1]

        MDSeparator:
            height: "1dp"

        MDLabel:
            text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"

Class code:

class NScreen(Screen):

    def on_start(self):
        self.ids.box.add_widget(
            MDLabel(
                text="{name_style} style",
                halign="center",
            ))

Thanks.

Elliot Garbus

unread,
Jan 26, 2021, 4:02:09 PM1/26/21
to kivy-...@googlegroups.com

It is much easier when you ask question if you provide a minimal runnable example.

 

Looking are your code

In your python code add an appropriate property for each data type you want to display ie:

 

class NScreen(Screen):

     title = StringProperty()

     text_content = StringProperty()

 

in your kv code have the kv code use these properties

<NScreen>:

    

    MDCard:

        orientation: "vertical"

        padding: "8dp"

        size_hint: None, None

        size: "280dp", "180dp"

        pos_hint: {"center_x": .5, "center_y": .5}

 

        MDLabel:

            text: "root.text"

            theme_text_color: "Secondary"

            size_hint_y: None

            height: self.texture_size[1]

 

        MDSeparator:

            height: "1dp"

 

        MDLabel:

            text: "text_content"

 

 

In your python code set the properties, for example:

class NScreen(Screen):

    title = StringProperty()

    text_content = StringProperty()

 

    def set_title(self, t)

        self.title = t

 

If you are instancing the NScreen instances in python you can use keyword arguments:

    card_screen = NScreen(text=’This is the title’, text_content=’This is the content’)

--
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/c8822920-2cae-4ecc-a5b3-67b3d88823aco%40googlegroups.com.

 

kivy

unread,
Jan 26, 2021, 4:12:45 PM1/26/21
to Kivy users support
Thanks for your reply. Now my code is:

<NScreen>:

    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: None, None
        size: "280dp", "180dp"
        pos_hint: {"center_x": .5, "center_y": .5}
 
        MDLabel:
            text: "root.text"
            theme_text_color: "Secondary"
            size_hint_y: None
            height: self.texture_size[1]
 
        MDSeparator:
            height: "1dp"
 
        MDLabel:
            text: "text_content"

class NScreen(Screen):

    title = StringProperty()
    text_content = StringProperty()

    def set_title(self, t):
        con = sqlite3.connect('test.db')
        cur = con.cursor()
        cur.execute("SELECT * FROM news ORDER BY id ASC")
        table = cur.fetchall()

        for line in table:
            title, news_content = line
            card_screen = NScreen(text=title, text_content=news_content)
            self.title = card_screen

Only show "root.text" and "text_content". It's don't fetch data from database. Where is the problem?

27 Ocak 2021 Çarşamba 00:02:09 UTC+3 tarihinde ElliotG yazdı:

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Jan 26, 2021, 4:24:23 PM1/26/21
to kivy-...@googlegroups.com

Issues highlighted below

Do you have a place to call set_title or do you need this called by an event?

 

Please post a minimal runnable example.

 

<NScreen>:

 

    MDCard:

        orientation: "vertical"

        padding: "8dp"

        size_hint: None, None

        size: "280dp", "180dp"

        pos_hint: {"center_x": .5, "center_y": .5}

 

        MDLabel:

            text: root.text # not "root.text"

            theme_text_color: "Secondary"

            size_hint_y: None

            height: self.texture_size[1]

 

        MDSeparator:

            height: "1dp"

 

        MDLabel:

            text: root.text_content # "text_content"

 

class NScreen(Screen):

 

    title = StringProperty()

    text_content = StringProperty()

 

    def set_title(self):  # you are not using t (self, t): 

        con = sqlite3.connect('test.db')

        cur = con.cursor()

        cur.execute("SELECT * FROM news ORDER BY id ASC")

        table = cur.fetchall()

 

        for line in table:

            title, news_content = line

            # card_screen = NScreen(text=title, text_content=news_content)  # do not instance the class inside the class

            self.title = title # card_screen

            self.text_content = news_content

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/caa2f84f-5baf-4201-a3d0-3a61c26f8a6ao%40googlegroups.com.

 

kivy

unread,
Jan 26, 2021, 4:40:06 PM1/26/21
to Kivy users support
I make changes and fixed issues but still card item title and text_content area is seems empty (database table is have a data).

27 Ocak 2021 Çarşamba 00:24:23 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 26, 2021, 4:44:42 PM1/26/21
to kivy-...@googlegroups.com

Post a minimal runnable example.  You could also replace the database with a list to see if the issue is the database or the display of the data.

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/73b9cc6a-a9b3-4ab9-996b-d0d0ea347cfbo%40googlegroups.com.

 

kivy

unread,
Jan 26, 2021, 4:48:31 PM1/26/21
to Kivy users support
Sure.

<NScreen>:

    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: None, None
        size: "280dp", "180dp"
        pos_hint: {"center_x": .5, "center_y": .5}
 
        MDLabel:
            text: root.title
            theme_text_color: "Secondary"
            size_hint_y: None
            height: self.texture_size[1]
 
        MDSeparator:
            height: "1dp"
 
        MDLabel:
            text: root.text_content
            theme_text_color: "Secondary"

class NScreen(Screen):

    title = StringProperty()
    text_content = StringProperty()

    def set_title(self):
        con = sqlite3.connect('test.db')
        cur = con.cursor()
        cur.execute("SELECT * FROM news ORDER BY id ASC")
        table = cur.fetchall()

        for line in table:
            title, news_content = line
            self.title = title
            self.text_content = news_content

I tried without database data but still seems nothing.


27 Ocak 2021 Çarşamba 00:44:42 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 26, 2021, 4:50:54 PM1/26/21
to kivy-...@googlegroups.com

When I say a minimal runnable example, I mean code I can 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/bc22ffd8-b355-4416-bc5e-d9e5b0b2d1ffo%40googlegroups.com.

 

kivy

unread,
Jan 26, 2021, 4:55:37 PM1/26/21
to Kivy users support
Here it is:

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sqlite3
from kivymd.app import MDApp
from kivy.properties import ListProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.label import MDLabel

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'
            NScreen:
                id: news
                name: 'news'
                
        MDNavigationDrawer:
            id: nav_drawer
            ScrollView:
                MDList:
                    OneLineListItem:
                        icon: 'soccer'
                        text: "Home"
                        on_press:
                            nav_drawer.set_state("close")
                            screen_manager.current = "home"
                    OneLineListItem:
                        text: "News"
                        on_press:
                            nav_drawer.set_state("close")
                            screen_manager.current = "news"

<HomeScreen>:
    MDLabel:
        text: 'Home screen'
        halign: 'center'



<NScreen>:

    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: None, None
        size: "280dp", "180dp"
        pos_hint: {"center_x": .5, "center_y": .5}
 
        MDLabel:
            text: root.title
            theme_text_color: "Secondary"
            size_hint_y: None
            height: self.texture_size[1]
 
        MDSeparator:
            height: "1dp"
 
        MDLabel:
            text: root.text_content
            theme_text_color: "Secondary"
'''


class HomeScreen(Screen):
    pass



class NScreen(Screen):

    title = StringProperty()
    text_content = StringProperty()

    def set_title(self):
        con = sqlite3.connect('test.db')
        cur = con.cursor()
        cur.execute("SELECT * FROM news ORDER BY id ASC")
        table = cur.fetchall()

        for line in table:
            title, news_content = line
            self.title = title # card_screen
            self.text_content = news_content

class ScrManage(ScreenManager):
    pass


class Sports(MDApp):
    def build(self):
        return Builder.load_string(kv)


Sports().run()



27 Ocak 2021 Çarşamba 00:50:54 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 26, 2021, 6:13:22 PM1/26/21
to kivy-...@googlegroups.com
Nothing was calling your code… Give this a try – then change back to using your database.
 
 
'''
    on_pre_enter: self.set_title()

    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: None, None
        size: "280dp", "180dp"
        pos_hint: {"center_x": .5, "center_y": .5}

        MDLabel:
            text: root.title
            theme_text_color: "Secondary"
            size_hint_y: None
            height: self.texture_size[1]

        MDSeparator:
            height: "1dp"

        MDLabel:
            text: root.text_content
            theme_text_color: "Secondary"
'''


class HomeScreen(Screen):
   
pass


class
NScreen(Screen):
    title = StringProperty()
    text_content = StringProperty()

   
def set_title(self
):
       
# con = sqlite3.connect('test.db')
        # cur = con.cursor()
        # cur.execute("SELECT * FROM news ORDER BY id ASC")
        # table = cur.fetchall()
       
table = [['And Now This...', 'The News goes here! A series of fascinating facts!']]
       
for line in table:
            title, news_content = line
           
self.title = title  # card_screen
           
self.text_content = news_content


class ScrManage(ScreenManager):
   
pass


class
Sports(MDApp):
   
def build(self):
       
return Builder.load_string(kv)


Sports().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/29e17e3c-2fc8-45b4-bcf6-498ac45f01b3o%40googlegroups.com.

 

kivy

unread,
Jan 27, 2021, 8:33:01 AM1/27/21
to Kivy users support
Thanks, I fixed problem. But I want to list card items. Now they are overlap.

kv area is:

<NScreen>:
    on_pre_enter: self.set_title()
    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: None, None
        size: "280dp", "180dp"
        pos_hint: {"top": .95, "left": .9}
 
        MDLabel:
            text: root.title
            theme_text_color: "Secondary"
            size_hint_y: None
            height: self.texture_size[1]
 
        MDSeparator:
            height: "1dp"
 
        MDLabel:
            text: root.text_content
            theme_text_color: "Secondary"

code is:

class NScreen(Screen):

    title = StringProperty()
    text_content = StringProperty()

    def set_title(self):
        con = sqlite3.connect('test.db')
        cur = con.cursor()
        cur.execute("SELECT * FROM news")
        table = cur.fetchall()
        for line in table:
            id, title, content = line
            self.title = title
            self.text_content = content



27 Ocak 2021 Çarşamba 02:13:22 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 27, 2021, 9:23:07 AM1/27/21
to kivy-...@googlegroups.com

If you want multiple cards in a list, you will need to create a ScrollView, use a layout to hold multiple cards, and instance a new card for each line in the database response.

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/b2252e3b-49a2-4d88-affd-bd68a4ccc621o%40googlegroups.com.

 

kivy

unread,
Jan 27, 2021, 10:21:34 AM1/27/21
to Kivy users support
I updated my kv code like this:

<NScreen>:
    on_pre_enter: self.set_title()
    ScrollView:
        MDCard:
            orientation: "vertical"
            padding: "8dp"
            size_hint: None, None
            size: "280dp", "180dp"
            pos_hint: {"top": .95, "left": .9}
    
            MDLabel:
                text: root.title
                theme_text_color: "Secondary"
                size_hint_y: None
                height: self.texture_size[1]
    
            MDSeparator:
                height: "1dp"
    
            MDLabel:
                text: root.text_content
                theme_text_color: "Secondary"

But gives error. I didn't changed my code, changed only kv area. Where is the problem?

27 Ocak 2021 Çarşamba 17:23:07 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 27, 2021, 10:39:07 AM1/27/21
to kivy-...@googlegroups.com
You need to put a layout under the scrollview that will hold the cards, a box layout will work. In Python you will need to instance the cards and add them to the layout. 

Sent from my iPhone

On Jan 27, 2021, at 8:21 AM, kivy <testo...@gmail.com> wrote:


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/d3784141-0845-4f97-8de0-5dfc45dcba18o%40googlegroups.com.

kivy

unread,
Jan 27, 2021, 11:04:08 AM1/27/21
to Kivy users support
Can you give me example please for python code?


27 Ocak 2021 Çarşamba 18:39:07 UTC+3 tarihinde ElliotG yazdı:

kivy

unread,
Jan 28, 2021, 10:39:03 AM1/28/21
to Kivy users support
Anybody help me please?

27 Ocak 2021 Çarşamba 19:04:08 UTC+3 tarihinde kivy yazdı:

Elliot Garbus

unread,
Jan 28, 2021, 11:05:55 AM1/28/21
to kivy-...@googlegroups.com

Here you go:

Key changes:

  • Create a separate class for the MDCard called InfoCard, need to be able to dynamically create these cards.
  • Add a ScrollView to the screen, and a BoxLayout to hold the cards.
  • In the Python code instance the InfoCards and add then to the BoxLayout in the Scrollview.

 

 

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sqlite3
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
'''
<InfoCard>:  # put the MD Card in a seperate class

    orientation: "vertical"
    padding: "8dp"
    size_hint: None, None
    size: "280dp", "180dp"
    pos_hint: {"center_x": .5, "center_y": .5}

    MDLabel:
        text: root.title
        theme_text_color: "Secondary"
        size_hint_y: None
        height: self.texture_size[1]

   MDSeparator:
        height: "1dp"

    MDLabel:
        text: root.text_content
        theme_text_color: "Secondary"


<NScreen>:
    on_pre_enter: self.set_title()
    ScrollView:
        do_scroll_x: True
        do_scroll_y: False
        scroll_type: ['bars', 'content']
        bar_width: '10dp'
        BoxLayout:
            padding: '20dp'
            spacing: '20dp'
            id: card_box
            width: self.minimum_width
            size_hint_x: None
   
    # MDCard:
    #     orientation: "vertical"
    #     padding: "8dp"
    #     size_hint: None, None
    #     size: "280dp", "180dp"
    #     pos_hint: {"center_x": .5, "center_y": .5}
    #
    #     MDLabel:
    #         text: root.title
    #         theme_text_color: "Secondary"
    #         size_hint_y: None
    #         height: self.texture_size[1]
    #
    #     MDSeparator:
    #         height: "1dp"
    #
    #     MDLabel:
    #         text: root.text_content
    #         theme_text_color: "Secondary"
'''


class HomeScreen(Screen):
   
pass


class
InfoCard(MDCard):

    title = StringProperty()
    text_content = StringProperty()


class NScreen(Screen):

   
def set_title(self):
       
# con = sqlite3.connect('test.db')

        # cur = con.cursor()
        # cur.execute("SELECT * FROM news ORDER BY id ASC")
        # table = cur.fetchall()
        table = [['And Now This...0', 'The News goes here! A series of fascinating facts!'],
                 [
'And Now This...1', 'The News goes here! A series of fascinating facts!'],
                 [
'And Now This...2', 'The News goes here! A series of fascinating facts!'],
                 [
'And Now This...3', 'The News goes here! A series of fascinating facts!'],
                 [
'And Now This...4', 'The News goes here! A series of fascinating facts!'],
                 ]
       
for line in table:
            title, news_content = line
           
self.ids.card_box.add_widget(InfoCard(title=title, text_content=news_content))


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/c7d4fee0-9405-45a9-a80f-28de7a56f1fdo%40googlegroups.com.

 

kivy

unread,
Jan 28, 2021, 2:55:52 PM1/28/21
to Kivy users support
Thank you so much! It's working.

28 Ocak 2021 Perşembe 19:05:55 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 28, 2021, 4:13:32 PM1/28/21
to kivy-...@googlegroups.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/1729281f-cd32-451b-867d-75ca9a23cb31o%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages