Home > Net >  How to Create List For loop Using .kv File
How to Create List For loop Using .kv File

Time:12-09

I am currently creating a project using kivy but i am stuck on a certain area. I want to display data using list view format using for loop but when i am applying this condition inside the for loop to my function, the data i want to display on is not displaying.

example.py

import kivy
kivy.require('1.9.2')

from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window   
from kivy.app import App
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.clock import Clock
from kivymd.uix.boxlayout import MDBoxLayout

from kivymd.uix.behaviors import HoverBehavior
from kivy.uix.button import Button
from kivy.properties import ListProperty
from kivy.animation import Animation
from kivymd.uix.list import OneLineListItem
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty
from kivy.config import Config

Window.size = (350,600)
class ListView(OneLineListItem):
    def on_start(self):
        for i in range(20):
            self.root.ids.container.add_widget(OneLineListItem(text=f"Single-line item {i}"))

class HoverButton(Button, HoverBehavior):
    background = ListProperty((0, 154/255, 254/255, 1))

    def on_enter(self):
        self.background = (0, 77/255, 128/255, 1)
        # Animation(size_hint=(.6, .1), d=0.3).start(self)
    def on_leave(self):
        self.background = (0, 154/255, 254/255, 1)
class AndroidApp(MDApp):
    def build(self):
        global screen_manager
        screen_manager = ScreenManager()
        
        screen_manager.add_widget(Builder.load_file("home.kv"))

        return screen_manager
AndroidApp().run()

my.kv

<ListView>:
    ScrollView:
        MDList:
            id: container
            OneLineListItem:
                text: "Three-line item with avatar"
            
            OneLineListItem:
                text: "Three-line item with avatar"
                

MDScreen:
    name: "home"
    MDBoxLayout:
        orientation: "vertical"
        #-------------------------TOOLBAR HEADER---------------------
        MDTopAppBar:
            pos_hint: {"top": 1}
            title: ""
            elevation: 4
            md_bg_color: "#cc0000"
            left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]

        MDBoxLayout:
            orientation: "horizontal"
            md_bg_color: 255/255, 255/255, 179/255, 1

            MDScreenManager:
                id: screen_manager
            

                #-------------------------SCREEN 2 NOTIFICATION---------------------            
                MDScreen:
                    name: "notif"
                                        ListView:
                #-------------------------SCREEN 1 ROOM---------------------
                MDScreen:
                    name: "room"
                    MDLabel:
                        text: "Room"
                        halign: 'center'
                        font_size: "25sp"
                        bold: True
                        color: 0, 0, 0, 1
                        font: "Poppins-Medium.tff"
                        pos_hint: {"center_x": .5, "center_y": .94}
                    HoverButton:
                        text: "401"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-SemiBold.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .3, "center_y": .80}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "402"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .7, "center_y": .80}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "403"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .3, "center_y": .67}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "404"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .7, "center_y": .67}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "405"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .3, "center_y": .54}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "406"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .7, "center_y": .54}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "407"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .3, "center_y": .41}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "409"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .7, "center_y": .41}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "411"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .3, "center_y": .28}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "412"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .7, "center_y": .28}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "413"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .3, "center_y": .15}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"
                    HoverButton:
                        text: "414"
                        background_color: 0, 0, 0, 0
                        font: "Poppins-Medium.tff"
                        color: 0, 0, 0, 1
                        font_size: 30
                        bold: True
                        size_hint: .3, .08
                        pos_hint: {"center_x": .7, "center_y": .15}
                        canvas.before:
                            Color:
                                rgb: self.background
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [8]
                        on_release:
                            screen_manager.current = "notif"

My UI

enter image description here

I want to solve my problem. Thank you for those who will answer my question.

CodePudding user response:

The main issue is that you are trying to use a OneLineListItem as a container. Try changing:

class ListView(OneLineListItem):

to something like:

class ListView(FloatLayout):
  • Related