Kivy不使用操作栏上的按钮更改屏幕

时间:2018-08-27 08:39:15

标签: python python-3.x kivy kivy-language

我引用的代码来自ikolim的答案:Why is my Kivy Actionbar gone?

这是我的python代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.window import Window

Window.clearcolor = (1,1,1,1)

Builder.load_file("designdawkalamomarunong.kv")


class Menu(BoxLayout):
    manager = ObjectProperty(None)

class Add(Screen):
    pass

class Help(Screen):
    pass

class Credits(Screen):
    pass

class Manager(ScreenManager):
    addScreen = ObjectProperty(None)
    helpScreen = ObjectProperty(None)
    creditsScreen = ObjectProperty(None)

class AntsApp(App):

    def build(self):
        return Menu()


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

这是猕猴桃代码:

<Menu>:
    manager: screen_manager
    orientation: "vertical"
    ActionBar:
        size_hint_y: 0.1
        ActionView:
            ActionPrevious:
            ActionButton:
                text: "Add"
                on_release: app.root.current = "add"
            ActionButton:
                text:"Help"
                on_release: app.root.current = "help"
            ActionButton:
                text: "Credits"
                on_release: app.root.current = "credits"
    Manager:
        id: screen_manager

<Add>:
    text: "hello boi"

<Help>:
    text: "hello world"

<Credits>:
    text: "goodbye world lol"

<Manager>:
    id: screen_manager
    addScreen: addScreen
    helpScreen: helpScreen
     creditsScreen: creditsScreen

    Add:
        id: addScreen
        name: 'add'
        manager: screen_manager

    Help:
        id: helpScreen
        name: 'help'
        manager: screen_manager

    Credits:
        id: creditsScreen
        name: 'credits'
        manager: screen_manager

当我运行它时,这是输出: Program output

当我单击操作栏上的按钮时,它不会返回任何内容。我还应该添加其他内容吗?预先谢谢你:)

编辑: 尝试这样做:

<Add>:
    Button:
        text: "hello boi"

它有效,但是当我尝试这样做时:

<Add>:
    Label:
        text: "hello boi"

它不起作用。我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

问题

kv文件

  1. app.root.current替换所有出现的root.manager.current,因为rootBoxLayout小部件,而不是ScreenManager小部件。
  2. 每个屏幕在Label之前都缺少text小部件。
  3. 在“屏幕”的每个类规则中将“按钮”小部件实例化为子级时,该文本都是可见的。但是,当将Button小部件替换为Label时,该文本将不可见。原因是因为Label的默认颜色文本是白色(rgba=1, 1, 1, 1),而BoxLayout的颜色也是白色。因此,您将无法看到文本。如果使用“标签”小部件,请添加color: (0, 0, 0, 1) # black color text

示例

kv文件

#:kivy 1.11.0

<Menu>:
    manager: screen_manager
    orientation: "vertical"
    ActionBar:
        size_hint_y: 0.1
        ActionView:
            ActionPrevious:
            ActionButton:
                text: "Add"
                on_release: root.manager.current = "add"
            ActionButton:
                text:"Help"
                on_release: root.manager.current = "help"
            ActionButton:
                text: "Credits"
                on_release: root.manager.current = "credits"
    Manager:
        id: screen_manager

<Add>:
    Label:
        text: "hello boi"
        color: 0, 0, 0, 1   # black color text

<Help>:
    Label:
        text: "hello world"
        color: 0, 0, 0, 1   # black color text

<Credits>:
    Label:
        text: "goodbye world lol"
        color: 0, 0, 0, 1   # black color text

<Manager>:
    addScreen: addScreen
    helpScreen: helpScreen
    creditsScreen: creditsScreen

    Add:
        id: addScreen
        name: 'add'

    Help:
        id: helpScreen
        name: 'help'

    Credits:
        id: creditsScreen
        name: 'credits'

输出

Img01