将 Kivy 列表项中的 on_release 绑定到函数

Posted

技术标签:

【中文标题】将 Kivy 列表项中的 on_release 绑定到函数【英文标题】:Bind on_release in Kivy List item to function 【发布时间】:2021-06-05 00:00:55 【问题描述】:

我正在构建一个从网络流式传输的音乐播放器,我想在链接网址之前查看它是否先在本地加载

我希望load_songs(path) 浏览列表并将歌曲名称绑定到用户点击的歌曲名称;

换句话说,我想将歌曲的标题绑定到列表项的“id”或属性。我想用 on_release 来播放这首歌。 使用当前代码,我得到一个未定义的错误。

感谢任何帮助

.py 文件

from kivymd.app import MDApp
from kivymd.icon_definitions import md_icons
from kivymd.uix.list import OneLineListItem, MDList
from kivy.properties import StringProperty
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager, ThemableBehavior
from kivy.properties import ObjectProperty

#pygame
import pygame
pygame.mixer.init()

path= "C://abapp3"

class MainApp(MDApp):
    def build(self):
        #self.theme_cls.theme_style = "Dark"
        #self.theme_cls.primary_palette = "Indigo"
        #self.theme_cls.accent_palette = "Teal"
        return



def load_songs(path):
    songs = []
    for filename in os.listdir(path):
            if filename.endswith('.wav'):
                songs.append(os.path.join(path,filename))
                self.root.ids.scroll.add_widget(OneLineListItem(text='Genesis', on_release=self.play_song))

    return songs


songs= load_songs(path)

pygame.mixer.music.load(songs[0])

def play_song():
    pygame.mixer.music.play()
    print('play:', "Genesis")


MainApp().run()

.kv

Screen:

    NavigationLayout:

        ScreenManager:

            MDScreen:

                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "Chapters"
                        font_style: "Caption"
                        elevation: 8
                        left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]

                    Widget:

                BoxLayout
                    MDLabel
                        id: "genesis"
                        MDList
                            id: "scroll"
                            OneLineListItem
                                text: "Genesis"
                                on_release:




        MDNavigationDrawer:
            id: nav_drawer
            BoxLayout
                orientation: "vertical"
                padding: "8dp"
                spacing: "8dp"


                MDLabel:
                    text: "Options"
                    font_style: "Button"
                    size_hint_y: None
                    height: self.texture_size[1]

                MDLabel:
                    text: "About"
                    font_style: "Caption"
                    size_hint_y: None
                    height: self.texture_size[1]

                ScrollView:
                    MDList:
                        OneLineIconListItem
                            text: 'Storage'
                            IconLeftWidget
                                icon: 'tools'


                        OneLineIconListItem
                            text: 'Choose Voice'
                            IconLeftWidget
                                icon: 'toolbox'

                        OneLineIconListItem
                            text: 'About'
                            IconLeftWidget
                                icon: 'toolbox-outline'

【问题讨论】:

【参考方案1】:

您的代码没有运行有几个原因:

    id - 不能是字符串(id: scroll,而不是 "scroll"NavigationLayout - 没有这样的类了,使用MDNavigationLayout (pip install git+https://github.com/kivymd/KivyMD.git)
from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem
from kivy.lang import Builder
import pygame
import os

pygame.mixer.init()
path = os.getcwd()

KV = """
Screen:
    MDNavigationLayout:
        ScreenManager:
            MDScreen:
                MDBoxLayout:
                    orientation: "vertical"
                    MDToolbar:
                        title: "Chapters"
                        font_style: "Caption"
                        elevation: 8
                        left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]

                    Widget:

                MDBoxLayout:
                    MDList
                        id: scroll

        MDNavigationDrawer:
            id: nav_drawer
            MDBoxLayout:
                orientation: "vertical"
                padding: "8dp"
                spacing: "8dp"


                MDLabel:
                    text: "Options"
                    font_style: "Button"
                    size_hint_y: None
                    height: self.texture_size[1]

                MDLabel:
                    text: "About"
                    font_style: "Caption"
                    size_hint_y: None
                    height: self.texture_size[1]

                ScrollView:
                    MDList:
                        OneLineIconListItem
                            text: 'Storage'
                            IconLeftWidget
                                icon: 'tools'

                        OneLineIconListItem:
                            text: 'Choose Voice'
                            IconLeftWidget:
                                icon: 'toolbox'

                        OneLineIconListItem:
                            text: 'About'
                            IconLeftWidget:
                                icon: 'toolbox-outline'
"""


class MainApp(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        songs = self.load_songs(path)
        pygame.mixer.music.load(songs[0])

    def load_songs(self, path_):
        songs = []
        for filename in os.listdir(path_):
            if filename.endswith('.mp3'):
                songs.append(os.path.join(path_, filename))
                self.root.ids.scroll.add_widget(OneLineListItem(text='Genesis', on_release=self.play_song))

        return songs

    @staticmethod
    def play_song(*args):
        pygame.mixer.music.play()
        print('play:', "Genesis")


MainApp().run()

【讨论】:

谢谢。让事情变得更清楚。但仍然有这个错误 kivy.factory.FactoryException: Unknown class 升级kivymdgit+https://github.com/kivymd/KivyMD.git 谢谢。没有更新仍然可以工作 我取得了一些进展,但也遇到了一些问题。非常感谢您的帮助---->***.com/questions/66709007/…

以上是关于将 Kivy 列表项中的 on_release 绑定到函数的主要内容,如果未能解决你的问题,请参考以下文章

向 kivy 按钮添加 on_release 操作

kivy 多次执行“on_release”命令

Kivy 滑块事件 on_release

动态创建的 kivy 按钮立即运行 on_press 和 on_release

kivy:单击 1 次按钮触发多个功能

通过 JSON 将背景图像填充到列表视图项中的问题