【问题标题】:Kivy Screen Manager Invalid data after declaration in KV fileKivy Screen Manager 在KV文件中声明后数据无效
【发布时间】:2016-01-25 21:54:56
【问题描述】:

在我的 kv 文件的第 34 行声明错误后,我收到一个无效数据。

'Invalid data after declaration')
ParserException: Parser: File "main.kv", line 34:  ...
   32: on_release: app.root.current = "newGame"
   33: MenuButton:
   34: text: "Load Game"
   35: on_release: app.root.current = "loadGame"
   36: MenuButton:  ...  Invalid data after declaration

我认为这个问题与屏幕管理器的实现有关。我最初是用纯 python 编写的,它运行良好。
我使用 kv 语言重写了主屏幕,它再次按预期工作。我将子菜单屏幕转换为 vk 语言,现在出现错误。有什么想法吗?
这是蟒蛇:

from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.core.audio import SoundLoader
from plyer import vibrator

presentation = Builder.load_file('main.kv')

backgroundMusic = SoundLoader.load('sounds/intro.mp3')
buttonSound = SoundLoader.load('sounds/button.mp3')
backgroundMusic.play()

class Layout(BoxLayout): pass
class MenuLayout(BoxLayout): pass
class MenuButton(Button): pass
class MainMenuScreen(Screen): pass
class NewGameMenuScreen(Screen): pass
class LoadGameMenuScreen(Screen): pass
class TutorialMenuScreen(Screen): pass
class SettingsMenuScreen(Screen): pass
class ScreenManagement(ScreenManager): pass

class Uranium235App(App):

    def menuButtonPressed(event):
        buttonSound.play()

        try:
            vibrator.vibrate(.125)
        except NotImplementedError: pass

    def build(self):
        return presentation

if __name__ == '__main__':
    Uranium235App().run()

这是 ky 文件:

#:import FadeTransition kivy.uix.screenmanager

<MenuButton>:
    on_press: app.menuButtonPressed()
    size_hint_y: .125
    background_normal: "images/button.png"
    background_down: "images/buttonPressed.png"

<MenuLayout>:
    orientation: "vertical"

<Layout>:
    orientation: "vertical"
    Image:
        source: "images/banner.png"

<ScreenManagement>:
    transition: FadeTransition()
    MainMenuScreen:
    NewGameMenuScreen:
    LoadGameMenuScreen:
    TutorialMenuScreen:
    SettingsMenuScreen:


<MainMenuScreen>:
    name: "main"
    Layout:
        MenuLayout:
            MenuButton:
                text: "New Game"
                on_release: app.root.current = "newGame"
                MenuButton:
                text: "Load Game"
                on_release: app.root.current = "loadGame"
                MenuButton:
                text: "Tutorial"
                on_release: app.root.current = "tutorial"
                MenuButton:
                text: "Settings"
                on_release: app.root.current = "settings"

<NewGameMenuScreen>:
    name: "newGame"
    Layout:
        MenuLayout:
            MenuButton:
                text: "Conquest Human VS Human"
                MenuButton:
                text: "Choas Human VS Human"
                MenuButton:
                text: "Conquest Human VS CPU"
                MenuButton:
                text: "Choas Human VS Human"

<LoadGameMenuScreen>:
    name: "loadGame"
    Layout:
        MenuLayout:
            MenuButton:
                text: "Conquest Human VS Human"
                MenuButton:
                text: "Choas Human VS Human"
                MenuButton:
                text: "Conquest Human VS CPU"
                MenuButton:
                text: "Choas Human VS Human"

<TutorialMenuScreen>:
    name: "tutorial"
    MenuLayout:
        MenuButton:
                text: "Conquest Mode"
            MenuButton:
                text: "Choas Mode"

<SettingsMenuScreen>:
    name: "settings"
    Layout:
        MenuLayout:
            MenuButton:
                text: "Music"
                MenuButton:
                text: "Sound Effects"
                MenuButton:
                text: "Vibration"
                MenuButton:
                text: "Choas Human VS Human"

【问题讨论】:

    标签: android python kivy


    【解决方案1】:

    MainMenuScreen 规则中的底部两行 MenuButton: 缩进了四个空格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多