Python-Kivy:'NoneType'对象没有属性'volume'

时间:2015-12-18 13:50:29

标签: python python-3.x kivy

我正在使用Kivy框架在Python 3.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:仍然为此疯狂,到目前为止我还没有到任何地方,如果有人可以帮助我,我仍然会离开。非常感谢!

0 个答案:

没有答案