gpt4 book ai didi

python - Kivy 使用 kv 文件中的 ScreenManager

转载 作者:行者123 更新时间:2023-12-01 04:12:23 25 4
gpt4 key购买 nike

我正在编写一个 kivy 程序/游戏。我以前也做过一个,不过总共只有一屏。我已经开始开发一个新程序,其中包含实现屏幕管理器的想法。我设法在 python 文件内构建了屏幕管理器,但它不适合我的长期使用,我想尝试一下屏幕管理器来自 .kv 文件

任何指导将不胜感激,不确定我错过了什么。我查看了这个问题(Kivy - Screen Manager - Accessing attribute in other class),并将我认为正确的内容拼凑在一起,但仍然无法加载屏幕 - 我觉得这是一个简单的答案,我很盲目......

main.py;

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition


class MenuScreen(Screen):
pass

class NewGameScreen(Screen):
pass

class LoadGameScreen(Screen):
pass

class ASCIILifeApp(App):
pass

if __name__ == "__main__":
ASCIILifeApp().run()

ASCIILife.kv;

#: kivy 1.9
#: import ScreenManager kivy.uix.screenmanager.ScreenManager
#: import Screen kivy.uix.screenmanager.ScreenManager
#: import NewGameScreen screen
#: import LoadGameScreen screen

ScreenManager:
id: screen_manager
#transition: FadeTransition()
MenuScreen:
id: menu_screen
name: 'MenuScreen'
manager: 'screen_manager'
NewGameScreen:
id: newgame_screen
name: 'NewGameScreen'
manager: 'screen_manager'
LoadGameScreen:
id: loadgame_screen
name: 'LoadGameScreen'
manager: 'screen_manager'

<MenuScreen>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'ASCII Life'
font_size: 50
Button:
text: 'New Game'
font_size: 30
on_release: app.root.current = 'newgame'
Button:
text: 'Load Game'
font_size: 30
on_release: app.root.current = 'loadgame'
Button:
text: 'Settings'
font_size: 30
on_release: app.root.current = 'something'
<NewGameScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Game Length in Days'
font_size: 30
BoxLayout:
orientation: 'horizontal'
ToggleButton:
text: '100'
state: 'down'
group: 'newgame_days'
font_size: 30
ToggleButton:
text: '200'
group: 'newgame_days'
font_size: 30
BoxLayout:
orientation: 'horizontal'
ToggleButton:
text: '365'
group: 'newgame_days'
font_size: 30
ToggleButton:
text: '3650'
group: 'newgame_days'
font_size: 30
ToggleButton:
text: 'Unlimited'
group: 'newgame_days'
font_size: 30
BoxLayout:
orientation: 'vertical'
size_hint: (1, .5)
Label:
text: 'Difficulty (Score Multiplier)'
font_size: 30
BoxLayout:
orientation: 'horizontal'
ToggleButton:
text: 'Easy (x1)'
state: 'down'
group: 'newgame_difficulty'
font_size: 30
ToggleButton:
text: 'Medium (x2.5)'
group: 'newgame_difficulty'
font_size: 30
ToggleButton:
text: 'Hard (x5)'
group: 'newgame_difficulty'
font_size: 30
<LoadGameScreen>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'load a game'
font_size: 30
Widget:
canvas:
Ellipse:
pos: self.pos
size: self.size
BoxLayout:
Button:
text: 'Menu'
font_size: 30
on_release: app.root.current = 'menu'
Button:
text: 'text'
font_size: 30

编辑:截断的 kv 文件

最佳答案

答案是使用构建器来构建 kv 文件。知道这很简单。文件如下;

main.py

#!/usr/bin/kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty


class MenuScreen(Screen):
pass

class NewGameScreen(Screen):
pass

class LoadGameScreen(Screen):
pass

class ScreenManager(ScreenManager):
pass

buildKV = Builder.load_file("ASCIILife.kv")

class ASCIILifeApp(App):
def build(self):
return buildKV

if __name__ == "__main__":
ASCIILifeApp().run()

关于python - Kivy 使用 kv 文件中的 ScreenManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34748579/

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