Binding button to class method in .py from .kv file

I have problems with classes and keep getting errors whatever I do, please tell me where I am wrong.
I have been trying to bind a button on first screen to open popup when it is pressed, but have this error:

AttributeError: 'str' object has no attribute 'bind'
   File "C:UsersLaraDownloadsKivy-1.9.0-py3.4-win32-x64kivy34kivylang.py", line 2012, in _apply_rule
     setattr(widget_set, key, value)
   File "kivyweakproxy.pyx", line 22, in kivy.weakproxy.WeakProxy.__setattr__ (kivyweakproxy.c:1189)
   File "kivyproperties.pyx", line 397, in kivy.properties.Property.__set__ (kivyproperties.c:4680)
   File "kivyproperties.pyx", line 429, in kivy.properties.Property.set (kivyproperties.c:5203)
   File "kivyproperties.pyx", line 484, in kivy.properties.Property.dispatch (kivyproperties.c:5852)
   File "kivy_event.pyx", line 1168, in kivy._event.EventObservers.dispatch (kivy_event.c:12154)
   File "kivy_event.pyx", line 1074, in kivy._event.EventObservers._dispatch (kivy_event.c:11451)
   File "C:UsersLaraDownloadsKivy-1.9.0-py3.4-win32-x64kivy34kivyuixpopup.py", line 190, in on_content
    self._container.add_widget(value)
   File "C:UsersLaraDownloadsKivy-1.9.0-py3.4-win32-x64kivy34kivyuixboxlayout.py", line 210, in add_widget
     widget.bind(

Relevant part of .py file:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.image import Image
from kivy.graphics import Color, Rectangle
from kivy.uix.popup import Popup
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.popup import Popup


class InstructionsPopup(Popup):
    pass

class StartScreen(Screen):

    def show_instructions(self):
        p = InstructionsPopup()
        p.open()

class GameScreen(Screen):
    pass

class RootScreen(ScreenManager):
    pass

class Main2App(App):
    def build(self):
        self.load_kv("main2.kv")
        return RootScreen()        

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

and .kv file:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<StartScreen>:
    name: "start"
    FloatLayout:
        Button:
            id: play
            text: "Play!"
            on_release: root.manager.current = 'game'
        Button:
            id: how
            text: "How to play"
            on_press: self.parent.parent.show_instructions()

<GameScreen>:
    name: "game"
    FloatLayout:
        SnakeWidget:
        InfoWidget: 
        Button:
            id: menu
            text: "Menu"

<InstructionsPopup>:
    size_hint: 0.5, 0.5
    content: "You start the game by ... "
    title: "How to play"

<RootScreen>:
    id: screen_manager
    transition: FadeTransition() 
    StartScreen:
        name: "start"
    GameScreen:
        name: "game"

I alse tried binding the button in StartScreen class like this:

btn = self.ids.how

btn.bind(on_press=ShowInstructions()),
but it kept telling me self is not defined.

Any ideas what else I can try or where I could be wrong?


Source: python

Leave a Reply