2014-09-03 63 views
2

在我的應用程序中,我有一個GameScreen,其中包含一些帶有文本的ClueButton1控件。按下該按鈕會將用戶帶到ClueScreen(使用kivy屏幕管理器),該按鈕有4個按鈕,每個按鈕都是答案選擇。當用戶在ClueScreen上按下正確的ClueAnswerButton1時,如何更改GameScreen上的ClueButton1的background_normal屬性?通過在另一個屏幕上按下按鈕更改Kivy按鈕控件屬性

我已經嘗試過分配ClueButton1 ID和使用DEF check_choice是()作爲GameScreen.cluebutton1id.background_normal = ......,但得到的錯誤:

AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal' 

main.py代碼是在這裏:

class GameScreen(Screen): 
    ... 


class ClueScreen(Screen): 
    ... 

    def check_choice(self): 

     if self.choice0.state == 'down': 
      if self.choice0.text == self.correct: 
       self.message.text = "[color=006600]Correct! Click back to game and keep" \ 
         "playing![/color]" 
       self.choice0.background_disabled_down = 'atlas://img/myatlas/green_button5' 
       self.choice0.disabled = True 
       self.choice1.disabled = True 
       self.choice2.disabled = True 
       self.choice3.disabled = True 
       return 
      else: 
       self.message.text = "Try again" 
       self.choice0.background_disabled_down = 'atlas://img/myatlas/red_button5' 
       self.choice0.disabled = True  
    ... 

而且.kv代碼是在這裏:

<GameScreen>: 
    GeneralFloatLayout: 
    GeneralAnchorLayout: 
     GeneralBoxLayout: 
      GameGridLayout: 
       ClueButton1: 
        text: root.question 
        on_press: root.manager.current = 'clue_screen'; 

<ClueScreen>: 
    message: message 
    choice0: choice0 
    choice1: choice1 
    choice2: choice2 
    choice3: choice3 

    ClueBoxLayout: 
     ClueLabel: 
      text: "[color=0046C3]" + "Put label Here" + "[/color]" 
     ClueMessage: 
      id: message 
     ClueAnswerButton1: 
      id: choice0 
      on_press: root.check_choice() 
     ClueAnswerButton1: 
      id: choice1 
      on_press: root.check_choice() 
     ClueAnswerButton1: 
      id: choice2 
      on_press: root.check_choice() 
     ClueAnswerButton1: 
      id: choice3 
      on_press: root.check_choice() 
     ClueGridLayout: 
      ReturnButton: 
       text: 'Back to game' 
       on_press: root.manager.current = 'game_home' 

回答

3

你試圖分配屬性在類對象上而不是實例。您可以通過ScreenManager獲得另一個屏幕的實例。假設您正在從根部件運行(即root.check_choice()):

self.manager.get_screen('game_home').cluebutton1id.background_normal = 'path/to/image.png' 
相關問題