gpt4 book ai didi

python - 通过在另一个屏幕中按下按钮来更改 Kivy 按钮小部件属性

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:18 24 4
gpt4 key购买 nike

在我的应用程序中,我有一个 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'

最佳答案

您正在尝试将属性分配给类对象而不是实例。您可以通过 ScreenManager 获取另一个屏幕的实例。假设您从根小部件(即 root.check_choice())运行它:

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

关于python - 通过在另一个屏幕中按下按钮来更改 Kivy 按钮小部件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25655147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com