簡體   English   中英

Kivy 標簽文本不會更新

[英]Kivy label text won't update

我對 python 並不陌生,但我對 Kivy 很陌生。 我不知道您需要回答我的代碼的哪些部分,但這是我的所有代碼:

主文件

from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.properties import StringProperty, NumericProperty
from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import ScreenManager, Screen

import os

class MainScreen(Screen):
    pass

class MainPage(BoxLayout): 
    sound = None
    playing = False
    song_text = StringProperty("None")
    
    volume_text = StringProperty("50")
    volume_num = NumericProperty(50)
    volume = 50 /100
    
    def on_button_1_click(self):
        if self.playing:
            self.sound.stop()
            self.sound = SoundLoader.load('music/Gold.mp3')
            self.song_text = "Gold"
            print("Gold")
            self.sound.play()
            self.sound.volume = self.volume
        else:
            self.sound = SoundLoader.load('music/Gold.mp3')
            self.song_text = "Gold"
            print("Gold")
    
    def on_button_2_click(self):
        if self.playing:
            self.sound.stop()
            self.sound = SoundLoader.load('music/Run For Your Life.mp3')
            self.song_text = "Run For Your Life"
            print("Run For Your Life")
            self.sound.play()
            self.sound.volume = self.volume
        else:
            self.sound = SoundLoader.load('music/Run For Your Life.mp3')
            self.song_text = "Run For Your Life"
            print("Run For Your Life")
    
    def on_button_3_click(self):
        if self.playing:
            self.sound.stop()
            self.sound = SoundLoader.load('music/Spirits Say.mp3')
            self.song_text = "Spirits Say"
            print("Spirits Say")
            self.sound.play()
            self.sound.volume = self.volume
        else:
            self.sound = SoundLoader.load('music/Spirits Say.mp3')
            self.song_text = "Spirits Say"
            print("Spirits Say")
    
    def on_play_button_click(self):
        if self.sound is not None:
            if not self.playing:
                #print("Sound found at %s" % self.sound.source)
                print("Sound start")
                self.sound.play()
                self.playing = True
                self.sound.volume = self.volume
            else:
                print("Song is playing")
        else:
            print("Choose a song")
    
    def on_stop_button_click(self):
        if self.sound is not None: 
            if self.playing:
                print("Sound stopped")
                self.sound.stop()
                self.playing = False
            else:
                print("Start playing a song")
        else:
            print("Choose a song")
    
    def volume_slider_value(self, widget):
        self.volume_text = str(int(widget.value))
        self.volume = round(widget.value/100,2)
        if self.playing:
            self.sound.volume = self.volume

class SongList(StackLayout):
    SONG_LIST = []
    for _,i in enumerate(os.listdir("music")):
        SONG_LIST.append(i.replace(".mp3","").capitalize())
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        for __,i in enumerate(self.SONG_LIST):
            size = dp(125)
            b = Button(text=str(i), size_hint=(None, None), size=(size, size))
            b.bind(on_press=self.Button_Click)
            self.add_widget(b)
    
    def Button_Click(self, widget):
        print("song: " + widget.text)
        if MainPage.playing:
            MainPage.sound.stop()
            path = os.path.join("music",(widget.text+".mp3"))
            MainPage.sound = SoundLoader.load(path)
            MainPage.song_text = widget.text
            MainPage.sound.play()
            MainPage.sound.volume = MainPage.volume
        else:
            path = os.path.join("music",(widget.text+".mp3"))
            MainPage.sound = SoundLoader.load(path)
            MainPage.song_text = widget.text
            print(widget.text)

class Playlists(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for i in range(0,10):
            size = dp(100)
            b = Button(text=str(i+1), size_hint=(None, None), size=(size, size))
            b.bind(on_press=self.playlist_button_pressed)
            self.add_widget(b)
    
    def playlist_button_pressed(self, widget):
        print(widget.text)
        app = App.get_running_app()
        app.root.transition.direction = 'down'
        app.root.current = 'playlist'

class PlaylistScreen(Screen):
    pass

class PlaylistSongs(BoxLayout):
    pass

class TunesApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(MainScreen(name='main'))
        sm.add_widget(PlaylistScreen(name='playlist'))
        
        return sm

if __name__ == '__main__':
    TunesApp().run()

曲調.kv

MainScreen:

<MainScreen>:
    id: mainscreen
    Pages:

<Pages@PageLayout>:
    MainPage:
    SongListScroll:
    BoxLayout:
        orientation: "vertical"
        canvas.before:
            Color:
                rgba: 0, 0, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: "PLAYLISTS"
            font_size: "50dp"
            size_hint: 1, .25
        Playlists:

<MainPage>:
    id: mainpage
    orientation: "vertical"
    canvas.before:
        Color:
            rgba: 0, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
    BoxLayout:
        orientation: "horizontal"
        id: favourites
        Button:
            text: "Gold"
            on_press: root.on_button_1_click()
            size_hint: .25, 1
        Button:
            text: "Run For Your Life"
            on_press: root.on_button_2_click()
            size_hint: .25, 1
        Button:
            text: "Spirits Say"
            on_press: root.on_button_3_click()
            size_hint: .25, 1
    BoxLayout:
        Label:
            id: song_label
            text: "Playing: " + root.song_text
        Slider:
            id: volume_sider
            min: 0
            max: 100
            value: 50
            step: 1
            orientation: "vertical"
            size_hint: .125, 1
            on_value: root.volume_slider_value(self)
            curser_size: "100dp", "100dp"
            color: .5, 0, 1, 1
        Label:
            text: root.volume_text
            size_hint: .125, 1
    BoxLayout:
        orientation: "horizontal"
        id: controls
        Button:
            text: "Play"
            on_press: root.on_play_button_click()
        Button:
            text: "Stop"
            on_press: root.on_stop_button_click()

<SongListScroll@ScrollView>:
    BoxLayout:
        orientation: "vertical"
        canvas.before:
            Color:
                rgba: 0, 0, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: "SONGS"
            font_size: "50dp"
            size_hint: 1, .25
        SongList:
            #size_hint: 1, None
            #height: self.minimum_height

<SongList>:
    orientation: "lr-tb"

<Playlists>:
    Button:
        text: "1"
        size_hint: None, None
        size: "100dp", "100dp"
        on_press:
            app.root.transition.direction = 'down'
            app.root.current = 'playlist'

<PlaylistScreen>:
    PlaylistSongs:

<PlaylistSongs>:
    orientation: "horizontal"
    Button:
        text: "Back"
        on_press:
            app.root.transition.direction = 'up'
            app.root.current = 'main'

id: song_label的標簽不會更新來自SongListButton_Click的文本。 我一直在互聯網上尋找答案,但找不到任何東西。 請幫忙。 也歡迎任何改進。

您不能像引用類變量一樣引用Property 您必須通過實例引用它。 因此,當您嘗試在MainPage中設置song_text時,您必須在 GUI 中獲取對MainPage實例的引用。 為了幫助解決這個問題,您可以在kv中添加一些ids

<MainScreen>:
    id: mainscreen
    Pages:
        id: pages

<Pages@PageLayout>:
    MainPage:
        id: mainpage
    SongListScroll:
    BoxLayout:
    .
    .
    .

然后,您可以使用這些新ids訪問py代碼中的MainPage實例:

App.get_running_app().root.get_screen('main').ids.pages.ids.mainpage.song_text = widget.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM