简体   繁体   English

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

[英]Kivy not changing screens with button on actionbar

The code I referred to is from ikolim's answer here: Why is my Kivy Actionbar gone?我提到的代码来自 ikolim 的回答: 为什么我的 Kivy Actionbar 不见了?

This is my python code:这是我的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()

And this is the kivy code:这是 kivy 代码:

<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

When I run it, this is the output:当我运行它时,这是输出: 程序输出

When I click the buttons on the actionbar, it does not return anything.当我单击操作栏上的按钮时,它不会返回任何内容。 Should I add anything else?我应该添加其他东西吗? Thank you in advance :)先感谢您 :)

Edit: Tried doing this:编辑:尝试这样做:

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

and it worked but when i tried this:它起作用了,但是当我尝试这样做时:

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

it did not work.这没用。 What am I missing here?我在这里缺少什么?

Problems问题

kv file kv文件

  1. Replace all occurrence of app.root.current with root.manager.current because root is a BoxLayout widget and not a ScreenManager widget.更换所有发生app.root.currentroot.manager.current因为root是一个BoxLayout小部件,而不是一个ScreenManager窗口小部件。
  2. Each screen is missing a Label widget before the text .每个屏幕在text之前都缺少一个Label小部件。
  3. When you instantiate Button widget as children in each class rule for the Screen, the text is visible.当您将 Button 小部件实例化为 Screen 的每个类规则中的子项时,文本是可见的。 But when you replace the Button widget with Label, the text is not visible.但是当您用 Label 替换 Button 小部件时,文本是不可见的。 The reason is because the default color text for Label is white ( rgba=1, 1, 1, 1 ) and the color of the BoxLayout is also white.原因是因为 Label 的默认颜色文本是白色( rgba=1, 1, 1, 1 )并且 BoxLayout 的颜色也是白色。 Therefore, you are not able to see the text.因此,您无法看到文本。 If using Label widget, add color: (0, 0, 0, 1) # black color text .如果使用标签小部件,请添加color: (0, 0, 0, 1) # black color text

Example例子

kv file 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'

Output输出

图像01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM