【问题标题】:How to switch screens in kivymd when there is a list有列表时如何在kivymd中切换屏幕
【发布时间】:2020-11-03 17:11:27
【问题描述】:

我创建了一个音乐播放器,当我单击它应该切换屏幕的列表时,我尝试添加切换屏幕的功能,但它给了我一个我根本不知道的错误。 它说:引发AttributeError(key),“on_press:root.manager.current ='controller'

也有问题

对不起,如果我的代码有点乱。

from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.core.audio import SoundLoader
from kivy.uix.screenmanager import Screen, ScreenManager
import os

helper_string = """
ScreenManager:
    MainScreen:
    PlayingScreen:

<MainScreen>:
    name: 'list'
    BoxLayout:
        orientation: "vertical"
        MDToolbar:
            title: "Demo music player"
        ScrollView:
            MDList:
                id: scroll
                on_press: root.manager.current ='controller'


<PlayingScreen>:
    name: 'controller'
    MDLabel:
        text : 'Hello world'
        halight: 'center'

"""

class MainScreen(Screen):
    pass


class PlayingScreen(Screen):
    pass


sm = ScreenManager()
sm.add_widget(MainScreen (name='list'))
sm.add_widget(PlayingScreen (name='controller'))

class MainApp(MDApp):
    def build(self):
        self.sound = None
        self.theme_cls.theme_style="Dark"
        screen = Builder.load_string(helper_string)
        return screen
        
    def on_start(self):
        for root, dirs, files in os.walk('assets'):
            for file in files:
                if file.endswith('.mp3'):
                    required_file = file
                    the_location = os.path.abspath(os.path.join(root, required_file))
                    self.root.ids.scroll.add_widget(OneLineListItem(text=the_location, 
                                                                    on_release=self.play_song))

    
    def play_song(self, onelinelistitem):
        the_song_path = onelinelistitem.text
        if self.sound:
            self.sound.stop()
        self.sound = SoundLoader.load(the_song_path)
        if self.sound:
            self.sound.play()
        print(the_song_path)

    

MainApp().run()

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    试试这个:self.root.current = "insert here the id of screen"

    【讨论】:

    • 您好,感谢您的回答,但您能具体说明一下我应该放在哪里吗,我不是高级程序员。
    • 更改:root.manager.current ='controller'app.root.current = 'controller'
    • 您的错误出现是因为 MDList 没有选项“on_press”试试这个:` on_touch_down: app.root.current ='controller'`为我工作
    • 现在它说:AttributeError: 'super' object has no attribute 'getattr' 我不知道该怎么做
    • 高质量的答案通常包括解释他们的代码如何/为什么解决 OP 的问题。我必须包含文档链接,以及使用限制。大多数更新来自未来的访问者,他们发现您的解释/见解有助于解决他们自己的编码问题。
    【解决方案2】:

    复制并粘贴这个:

    抱歉,我的回复迟了 Tou必须删除“on_press”:

    这是正确的代码: MDList: id: scroll on_touch_down: app.root.current ='controller'

    【讨论】:

    • 将“on_press”的行改为“on_touch_down: app.root.current ='controller'”
    • 我做了,只要去链接你就会看到错误是什么(snipboard.io/AMbFD6.jpg
    • 对不起,我的回答迟了,删除“on_press:”
    • 我照你说的做了,但还是一样[错误]:(snipboard.io/AMbFD6.jpg)
    • 是的,我以另一种方式做到了。我使用 MDDialog 而不是屏幕切换,但感谢您帮助我 :)
    【解决方案3】:

    我使用了on_release,它对我有用,而不是on_press..

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 2021-10-02
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多