我正在编写一个简单的程序/游戏。我以前做了一个,但总共只有一个屏幕。我已经开始制定一个新计划,其中包含实施Screen Manager
的想法。我设法在python文件中构建Screen Manager
,但它不适合我的长期使用,我想尝试Screen Manager
来自.kv file
}。
任何指导都将不胜感激,不确定我错过了什么。我查看了这个问题(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文件
答案 0 :(得分:1)
答案是使用构建器来构建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()
答案 1 :(得分:1)
详细说明Chazara的调查结果:
根据命名范围重命名kv代码文件:
Kivy会查找与您的App类同名的Kv文件 小写,减去“App”,如果它以'App'结尾。
或只需使用构建器函数显示它:
Builder.load_file('path/to/file.kv')
答案 2 :(得分:0)
详细阐述Chazara的调查结果: 我用kivy 1.10.1dev0和Python 3.6测试了Chazara的代码;我得到一个错误说:
kivy.uix.screenmanager.ScreenManagerException: No Screen with name "something".
修复它,我改变了.kv文件
[...]
Button:
text: 'New Game'
font_size: 30
on_release: app.root.current = 'newgame'
[...]
到:
[...]
Button:
text: 'New Game'
font_size: 30
on_release: app.root.current = 'NewGameScreen'
[...]
它引用了ScreenManager中给出的NewGameScreen的名称
ScreenManager:
[...]
NewGameScreen:
id: newgame_screen
*name: 'NewGameScreen'*
manager: 'screen_manager'