在 on_start() 函数 kivy Python 中使用 id
Use ids in on_start() function kivy Python
所以我正在构建一个常识问答游戏,在应用程序启动时,玩家的分数(保存在 .txt 文件中)应该显示在标签中。
我尝试为此使用 on_start() 函数,但我似乎无法访问 'score' 标签的 ID。这是给出错误的代码行:
self.root.get_screen("home_screen").ids.score.text = str(playerScore)
我收到以下错误:
AttributeError: 'super' object has no attribute '__getattr__'
这是我的完整代码:
#main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_file('design.kv')
class RootWidget(ScreenManager):
pass
class HomeScreen(Screen):
pass
class MainApp(App):
def build(self):
return RootWidget()
def on_start(self):
with open("score.txt", "r") as f:
playerScore = f.readline()
self.root.get_screen("home_screen").ids.score.text = str(playerScore)
if __name__ == "__main__":
MainApp().run()
和
#design.kv
<HomeScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 2
Button:
id: infoButton
text: "Wiki"
Label:
id: score
GridLayout:
cols: 1
Label:
id: Question
text: "Question"
Button:
id: Button1
text: "Option 1"
Button:
id: Button1
text: "Option 2"
Button:
id: Button1
text: "Option 3"
Button:
id: Button1
text: "Option 4"
<RootWidget>:
HomeScreen:
name: "home_screen"
还有一个 'score.txt' 文件,里面只有 - '100'。
谢谢。
你的做法是对的。我不确定你想要什么样的布局,但你犯了两个错误:
- 您的 .kv 代码在 class 定义后缺少缩进
- 即使有缩进,您还是将三个 GridLayout 放在了彼此的顶部。一种方法是将这三个 GridLayout 放在一个 BoxLayout 中,或者只是以另一种方式结构化您的 GridLayout(例如,将所有小部件打包在一个 GridLayout 中)。基本上你已经有了“Wiki”和“100”(分数)按钮,但它们只是在你的选项按钮后面,所以你几乎看不到它们。
#design.kv
<HomeScreen>:
BoxLayout:
GridLayout:
cols: 1
GridLayout:
cols: 2
Button:
id: infoButton
text: "Wiki"
Label:
id: score
GridLayout:
cols: 1
Label:
id: Question
text: "Question"
Button:
id: Button1
text: "Option 1"
Button:
id: Button1
text: "Option 2"
Button:
id: Button1
text: "Option 3"
Button:
id: Button1
text: "Option 4"
<RootWidget>:
HomeScreen:
name: "home_screen"
所以我正在构建一个常识问答游戏,在应用程序启动时,玩家的分数(保存在 .txt 文件中)应该显示在标签中。
我尝试为此使用 on_start() 函数,但我似乎无法访问 'score' 标签的 ID。这是给出错误的代码行:
self.root.get_screen("home_screen").ids.score.text = str(playerScore)
我收到以下错误:
AttributeError: 'super' object has no attribute '__getattr__'
这是我的完整代码:
#main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_file('design.kv')
class RootWidget(ScreenManager):
pass
class HomeScreen(Screen):
pass
class MainApp(App):
def build(self):
return RootWidget()
def on_start(self):
with open("score.txt", "r") as f:
playerScore = f.readline()
self.root.get_screen("home_screen").ids.score.text = str(playerScore)
if __name__ == "__main__":
MainApp().run()
和
#design.kv
<HomeScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 2
Button:
id: infoButton
text: "Wiki"
Label:
id: score
GridLayout:
cols: 1
Label:
id: Question
text: "Question"
Button:
id: Button1
text: "Option 1"
Button:
id: Button1
text: "Option 2"
Button:
id: Button1
text: "Option 3"
Button:
id: Button1
text: "Option 4"
<RootWidget>:
HomeScreen:
name: "home_screen"
还有一个 'score.txt' 文件,里面只有 - '100'。
谢谢。
你的做法是对的。我不确定你想要什么样的布局,但你犯了两个错误:
- 您的 .kv 代码在 class 定义后缺少缩进
- 即使有缩进,您还是将三个 GridLayout 放在了彼此的顶部。一种方法是将这三个 GridLayout 放在一个 BoxLayout 中,或者只是以另一种方式结构化您的 GridLayout(例如,将所有小部件打包在一个 GridLayout 中)。基本上你已经有了“Wiki”和“100”(分数)按钮,但它们只是在你的选项按钮后面,所以你几乎看不到它们。
#design.kv
<HomeScreen>:
BoxLayout:
GridLayout:
cols: 1
GridLayout:
cols: 2
Button:
id: infoButton
text: "Wiki"
Label:
id: score
GridLayout:
cols: 1
Label:
id: Question
text: "Question"
Button:
id: Button1
text: "Option 1"
Button:
id: Button1
text: "Option 2"
Button:
id: Button1
text: "Option 3"
Button:
id: Button1
text: "Option 4"
<RootWidget>:
HomeScreen:
name: "home_screen"