PythonKivy:'NoneType'对象没有属性'volume'

2024-09-27 23:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Kivy框架在python3.4中构建MP3播放器,除了音量控制滑块之外,一切似乎都在工作。在

每当我单击音量按钮时,它会返回给我:'NoneType' object has no attribute 'volume'。我想是因为声音设置为“无”。我仍然是Python和Kivy的新手,所以我正在努力从错误中吸取教训,代码可能是硬编码的,对此我很抱歉,我接受任何建设性的批评。在

代码有点长,所以我只提供有问题的部分。我把整个代码贴在了pastebin上,链接在下面。在

class MP3Win(BoxLayout):

   sound = None
   paused = False
   paused_position = 0

   def slider_vol(self):

    """slider volume to increase or decrease the volume
    of the sound manually with a popup box containing a
    a slider, however, if I don't put it under the 'if self.sound:'
    it keep giving me an attribute error NoneType has no attribute volume
    and it bother me because I cant open the volume slider
    unless there is a song playing."""

    slider = Slider(orientation='horizontal',min=0,max=1,value=self.sound.volume)
    popup = Popup(title='Speakers(Volume)',
                content=slider,
                size_hint=(None,None),size=(400,100),
                separator_height=1)
    popup.open()
    self.sound.bind(volume=slider.setter('value'))
    slider.bind(value=self.sound.setter('volume'))

    if self.sound.volume == 0:
        self.ids['vol_btn'].background_normal = 'atlas://data/images/defaulttheme/audio-volume-muted'
        self.ids['vol_btn'].background_down = 'atlas://data/images/defaulttheme/audio-volume-muted'
    elif self.sound.volume <= 0.3:
        self.ids['vol_btn'].background_normal = 'atlas://data/images/defaulttheme/audio-volume-low'
        self.ids['vol_btn'].background_down = 'atlas://data/images/defaulttheme/audio-volume-low'
    elif self.sound.volume <= 0.5:
        self.ids['vol_btn'].background_normal = 'atlas://data/images/defaulttheme/audio-volume-medium'
        self.ids['vol_btn'].background_down = 'atlas://data/images/defaulttheme/audio-volume-medium'
    elif self.sound.volume >= 0.7:
        self.ids['vol_btn'].background_normal = 'atlas://data/images/defaulttheme/audio-volume-high'
        self.ids['vol_btn'].background_down = 'atlas://data/images/defaulttheme/audio-volume-high'

class MP3Player(App):
   def build(self):
       return MP3Win()

if __name__ == "__main__":
   MP3Player().run()

Full code

Tl;Dr:为什么音量滑块会抛出'NoneType' object has no attribute 'volume',即使其余代码都可以正常工作?我快疯了!在

提前谢谢!在

编辑:还没有找到解决办法我已经试了几个小时,但我还是很难过,仍然在等待答案,如果可能的话,再次感谢Wander和Padraic!在

编辑2:还在为它疯狂,到目前为止我还没有取得任何进展,如果有人能帮我的话,我还是会把它打开。非常感谢!在


Tags: 代码selfidsdataattributeaudiobackgroundslider

热门问题