在我的应用程序中,我有一个GameScreen,其中包含带有一些文本的ClueButton1小部件。按下该按钮可将用户带到ClueScreen(使用kivy屏幕管理器),该按钮有4个按钮,每个按钮都是一个应答选择。当用户在ClueScreen上按下正确的ClueAnswerButton1时,如何在GameScreen上更改ClueButton1的background_normal属性?
我已经尝试将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'
答案 0 :(得分:3)
您尝试在类对象上分配属性而不是实例。您可以通过ScreenManager
获取另一个屏幕的实例。假设您从根小部件(即root.check_choice()
)运行此命令:
self.manager.get_screen('game_home').cluebutton1id.background_normal = 'path/to/image.png'