簡體   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