python - AttributeError:“kivy.properties.ObjectProperty”对象没有属性“background_normal”
问题描述
我正在用 kivy 制作 mp3 播放器,但我遇到了问题。我已经搜索了解决方案,但它们都不适合我。我收到此错误AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal'
,无法解决。请帮我。
我的python代码
class PlaySongScreen(Screen):
play_btn = ObjectProperty(None)
def play(self, song_path):
#function to play the desired song
if song_path == 'None':
song_path = songs_list[0]['text']
mypath = song_path
mypath = mypath.replace('\\', '\\\\')
if pg.mixer.music.get_busy() == 0:
#playing the song
pg.mixer.music.load(mypath)
pg.mixer.music.play()
#change the play button to pause button
self.play_btn.background_normal = 'images\\new_pause.jpg'
self.play_btn.background_down = 'images\\pause.jpg'
play_function = PlaySongScreen.play
class PlayButton(Factory.Button):
def on_press(self):
play_function(PlaySongScreen, song_path=self.text)
我的 .kv 代码
<PlaySongScreen>:
play_btn: play_pause
GridLayout:
cols: 1
padding: 5
RelativeLayout:
Button:
id: play_pause
on_press: root.play('None')
background_normal: 'images\\new_play.jpg'
background_down: 'images\\play.jpg'
size_hint: 0.8, 0.9
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
我得到的错误
File "c:/Users/user/Desktop/Coding/python/Projects/Music Player/music_player.py", line 126, in on_press
play_function(PlaySongScreen, song_path=self.text)
File "c:/Users/user/Desktop/Coding/python/Projects/Music Player/music_player.py", line 40, in play
self.play_btn.background_normal = 'images\\new_pause.jpg'
AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal'
请帮我解决这个错误。
解决方案
无法使用您发布的代码重现您的错误。但我怀疑在您使用的实际代码中PlayButton
,它调用了PlaySongScreen.play
. 如果那是您实际上正在做的事情,那么这就是您的问题的原因。如果您访问PlaySongScreen.play
,您访问的是ObjectProperty
,而不是Button
带有 id的play_pause
。您必须play_btn
通过 的实例而PlaySongScreen
不是类本身来访问属性。
您发布的代码的方式是正确的。使用Button
设置on_press
为root.play
。是的root
实例PlaySongScreen
。
推荐阅读
- php - 浏览器中的 PHP 错误未显示(使用 Docker)
- python - 如何从 save_formset 访问 Django 模型字段
- windows - 如何打印特定元素
- python-3.x - 如何调试此堆栈跟踪?(google.auth.transport.grpc.AuthMetadataPlugin)
- jsp - JSP会话变量没有从一个页面保存到另一个页面
- java - 我怎样才能返回数组?
- javascript - 如何在做某事之前确认所有复选框都被选中?
- javascript - 提交 JavaScript 后如何关闭模式
- swift - Swift - 使用 UIKeyboardWillChangeFrame 时关闭键盘并调用 TouchUpInside 按钮同时不起作用
- sql - 关键字“right”附近的语法不正确