2015-07-06 34 views
2

無論我做什麼,我都會遇到類問題並不斷收到錯誤,請告訴我我錯在哪裏。 我一直在試圖結合第一屏幕上的按鈕,打開彈出當它被按下,但有這樣的錯誤:從.kv文件的.py文件夾中的類方法的綁定按鈕

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" 

我ALSE試圖綁定按鈕這樣的類:

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

但它一直告訴我self沒有定義。

任何想法還有什麼我可以嘗試或在哪裏我可能是錯的?

回答

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

對不起,但我不明白我應該改變,爲什麼我會將floatlayout更改爲boxlayout?我已經設置了所有的位置和大小,它的工作原理沒問題,我只是刪除它的帖子,因爲它與按鈕綁定無關。我只想知道如何在.py中定義一些東西,這樣我就可以綁定按鈕並使用ID。我不斷收到錯誤,說明方法沒有定義或者沒有自己。 – Larisa

+0

實際上,爲了方便起見,我改變了它,在我提到可以使用floatlayout的評論中看到,我認爲你不知道如何解決這個問題,無論如何,你總是可以自由地使用任何你想要的佈局,所以現在爲'id'。轉到此鏈接。 http://stackoverflow.com/questions/30202801/how-to-access-id-widget-of-different-class-from-a-kivy-file-kv – kiok46

+0

在這個鏈接,請參閱這兩個答案。 http://stackoverflow.com/questions/30493441/kivy-color-change-with-button/30533704#30533704 – kiok46

相關問題