将按钮绑定到.kv文件中.py中的类方法

时间:2015-07-06 08:36:15

标签: python kivy

我遇到课程问题并且无论我做什么都会出错,请告诉我错在哪里。 我一直试图在第一个屏幕上绑定一个按钮,以便在按下时打开弹出窗口,但出现此错误:

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(

.py 文件的相关部分:

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()

.kv 文件:

#: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"

我尝试绑定StartScreen类中的按钮,如下所示:

btn = self.ids.how
btn.bind(on_press=ShowInstructions())

但它一直告诉我self没有定义。

任何想法我还能尝试什么或我可能出错的地方?

1 个答案:

答案 0 :(得分:2)

您的kivy文件中存在两个编码错误。

<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()