简体   繁体   English

MainApp().run() python 和 kivy

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

this probably has a simple answer, been struggling with this for some time.这可能有一个简单的答案,已经为此苦苦挣扎了一段时间。 I want to build some kind of music player that can stream and download.我想构建某种可以 stream 和下载的音乐播放器。 I'm also new to Kivy and somewhat new to Python我也是 Kivy 的新手,对 Python 有点陌生

I want to return the value of the ID from the Kivy file and match it with the same value from the list of songs.我想从 Kivy 文件中返回 ID 的值,并将其与歌曲列表中的相同值匹配。

But first I need to figure out why the print statements in Main.App class are not working.但首先我需要弄清楚为什么Main.App class 中的打印语句不起作用。 When I put the methods directly under the build(self) : method I get the print that the code is working but when I add them inside new method to get more flexibility the code runs and I dont get error messages However I dont know how to get items from the MainApp class to run.当我将方法直接放在build(self) : 方法下时,我得到代码正在运行的打印,但是当我将它们添加到新方法中以获得更大的灵活性时,代码运行并且我没有收到错误消息但是我不知道如何从MainApp class 获取项目以运行。 Conversely when I put the code as a functions above the MainApp class I get self or root related errors相反,当我将代码作为函数放在MainApp class 上方时,我得到了与selfroot相关的错误

The Kivy on_release call also does not do anything on the terminal ie does not print when play_song function is called Kivy on_release调用也不会在终端上执行任何操作,即在play_song function 时不会打印

I use pycharm for editing我使用 pycharm 进行编辑

please forgive my daft question.请原谅我愚蠢的问题。

Python from kivymd.app import MDApp from kivy.uix.screenmanager import ScreenManager, Screen import pygame import os 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 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'

The problem is the way you assign the function to the button.问题在于您将 function 分配给按钮的方式。 You forgot to add the parenthesis.你忘了加上括号。

The correct way would be:正确的方法是:

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

Now the function gets called and your print-statement will be executed.现在调用 function 并执行您的打印语句。

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

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