python - 有列表时如何在kivymd中切换屏幕
问题描述
我创建了一个音乐播放器,当我单击它应该切换屏幕的列表时,我尝试添加切换屏幕的功能,但它给了我一个我根本不知道的错误。它说:引发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()
解决方案
推荐阅读
- pytorch - 如何在 Pytorch 中测试自定义数据集?
- android - 我该如何解决这个错误“错误:控制字符 U+0000 只能在字符串和注释中使用。”
- reactjs - 在 React 中通过 props 传递对象数组
- python - 从seaborn kdeplot级别(2D)python中提取数据
- python - 使用 Python 计算网站中词频的代码未输出正确的频率
- html - 如何更改自定义收音机小部件代码以隐藏 Django 中的文本?
- c# - GroupBy 然后在实体框架中的单独表达式中求和
- arduino-c++ - afr 仪表不读取数字 Devide 问题?
- java - Spring Webflux:当 Mono.zip 中的一个 Mono 为空时处理错误
- r - 四舍五入到最接近的百分之一 dplyr