2

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:\Users\Lara\Downloads\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 2012, in _apply_rule
     setattr(widget_set, key, value)
   File "kivy\weakproxy.pyx", line 22, in kivy.weakproxy.WeakProxy.__setattr__ (kivy\weakproxy.c:1189)
   File "kivy\properties.pyx", line 397, in kivy.properties.Property.__set__ (kivy\properties.c:4680)
   File "kivy\properties.pyx", line 429, in kivy.properties.Property.set (kivy\properties.c:5203)
   File "kivy\properties.pyx", line 484, in kivy.properties.Property.dispatch (kivy\properties.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:\Users\Lara\Downloads\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\uix\popup.py", line 190, in on_content
    self._container.add_widget(value)
   File "C:\Users\Lara\Downloads\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\uix\boxlayout.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?

doru
  • 9,022
  • 2
  • 33
  • 43
Larisa
  • 781
  • 1
  • 11
  • 27

1 Answers1

2

There were two coding errors in your kivy file.

<StartScreen>:
    name: "start"
    BoxLayout:
    #Change your layout here
    #  OR
    #give pos or pos_hint or size_hint if you want to give floatlayout
        Button:
            id: play
            text: "Play!"
            on_release: root.manager.current = 'game'
        Button:
            id: how
            text: "How to play"
            on_press: root.show_instructions()
            #First error was here

<InstructionsPopup>:
    size_hint: 0.5, 0.5
    title: "How to play"
    #You add the content in the popup, this way
    Button:
        text: "dismiss"
        on_release: root.dismiss()
kiok46
  • 1,704
  • 1
  • 12
  • 28
  • Sorry but I don't understand what I should change, why would I change floatlayout to boxlayout? I have already set all the positions and sizes and it works okay, I just removed it for the post because it has nothing to do with button binding. I just want to know how to define something in .py so I can bind buttons and use ids. I keep getting errors that method is not defined or that it doesn't have self. – Larisa Jul 06 '15 at 19:37
  • Actually, I changed it for my convenience, see in the comment that I mentioned that you can use floatlayout too, I thought that you didn't know how to fix that, anyway, you are always free to use any layout you want, So now for the `id`. Go to this link. http://stackoverflow.com/questions/30202801/how-to-access-id-widget-of-different-class-from-a-kivy-file-kv – kiok46 Jul 07 '15 at 00:14
  • and in this link, see both the answers. http://stackoverflow.com/questions/30493441/kivy-color-change-with-button/30533704#30533704 – kiok46 Jul 07 '15 at 00:16
  • Thank you for your answers. It looks like I have problems with reading .kv file and nothing what I tried worked. I started it all over again and am trying to use more python coding now, and it seems it works better, but I find it harder to understand. – Larisa Jul 08 '15 at 07:58
  • This link shall end your fight with kivy file :) http://stackoverflow.com/questions/30430780/python-kivy-kv-file-wont-read/30534840#30534840 – kiok46 Jul 08 '15 at 08:29
  • Thanks :) Can you please look at post I am going to make now because it still does not work? I have been trying to include pong game on screen2 for almost a week now, with no success. Apparently I don't understand something about classes. – Larisa Jul 08 '15 at 09:09
  • yea! sure, post your question. – kiok46 Jul 08 '15 at 09:16