简体   繁体   中英

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. I'm also new to Kivy and somewhat new to 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.

But first I need to figure out why the print statements in Main.App class are not working. 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. Conversely when I put the code as a functions above the MainApp class I get self or root related errors

The Kivy on_release call also does not do anything on the terminal ie does not print when play_song function is called

I use pycharm for editing

please forgive my daft question.

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'

The problem is the way you assign the function to the button. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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