繁体   English   中英

MainApp().run() python 和 kivy

[英]MainApp().run() python and kivy

这可能有一个简单的答案,已经为此苦苦挣扎了一段时间。 我想构建某种可以 stream 和下载的音乐播放器。 我也是 Kivy 的新手,对 Python 有点陌生

我想从 Kivy 文件中返回 ID 的值,并将其与歌曲列表中的相同值匹配。

但首先我需要弄清楚为什么Main.App class 中的打印语句不起作用。 当我将方法直接放在build(self) : 方法下时,我得到代码正在运行的打印,但是当我将它们添加到新方法中以获得更大的灵活性时,代码运行并且我没有收到错误消息但是我不知道如何从MainApp class 获取项目以运行。 相反,当我将代码作为函数放在MainApp class 上方时,我得到了与selfroot相关的错误

Kivy on_release调用也不会在终端上执行任何操作,即在play_song function 时不会打印

我使用 pycharm 进行编辑

请原谅我愚蠢的问题。

Python from kivymd.app import MDApp from kivy.uix.screenmanager import ScreenManager, Screen import pygame import os

pygame.mixer.init()


class ChapterLayout(Screen):
    pass


class MainApp(MDApp):
    path = "C://abapp3"

    def build(self):
        return

    def load_songs(path):
        songs = []
        for filename in os.listdir(path):
            if filename.endswith('.wav'):
                print(filename)
                songs.append(os.path.join(path, filename))
                print(songs)

        return songs

    def play_song(self):
        return print("hallelujah")


MainApp().run()

KIVY

Screen
    NavigationLayout:
        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: Chapters
                        OneLineIconListItem
                            id: Genesis.wav
                            text: "Genesis"
                            on_release:app.play_song
                        OneLineIconListItem
                            id: Exodus
                            text: "Exodus"
                            on_release:
                        OneLineIconListItem
                            id: Leviticus
                            text: "Leviticus"
                            on_release:

        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'

问题在于您将 function 分配给按钮的方式。 你忘了加上括号。

正确的方法是:

OneLineIconListItem
id: Genesis.wav
text: "Genesis"
on_release :app.play_song()

现在调用 function 并执行您的打印语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM