Why is my Kivy Actionbar gone?

独自空忆成欢 提交于 2019-12-11 06:17:05

问题


Good evening,

I'm trying to combine kivy's actionbar with a screenmanager. I've gotten to the point where I can switch through screens, but am not able to get my actionbar to show. I've followed alot of examples, but none that can help me with my problem. I'm fairly new to kivy so I haven't been working with it for very long. I was kind of wondering if someone would be able to point out where my problem lies, because I'm trying to build my very own GUI with an action bar that lets me switch through screens. here is my main.py:

#!/usr/bin/env python3
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout

class Menu(BoxLayout):
    pass
class ScreenThermo(Screen):
    pass
class ScreenLight(Screen):
    pass
class ScreenEnergy(Screen):
    pass
class ScreenWeather(Screen):
    pass

class Manager(ScreenManager):
    screen_thermo = ObjectProperty(None)
    screen_light = ObjectProperty(None)
    screen_energy = ObjectProperty(None)
    screen_weather = ObjectProperty(None)

class MenuApp(App):
    def thermostaat(self):
        print("Thermostaat")
    def verlichting(self):
        print("Verlichting")
    def energie(self):
        print("Energie")
    def weer(self):
        print("Het Weer")
    def build(self):
        return Manager()


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

And here's my menu.kv file:

#:kivy 1.0.9
<Menu>:
    orientation: "vertical"

    ActionBar:
        ActionView:
            ActionPrevious:
            ActionButton:
                text: "Thermostaat"

                on_release: app.thermostaat()
            ActionButton:
                text: "Verlichting"
                #I want my screens to switch when clicking on this actionbar button
                on_press: root.manager.current= 'light'
                on_release: app.verlichting()
            ActionButton:
                text: "Energieverbruik"
                on_release: app.energie()
            ActionButton:
                text: "Het Weer"
                on_release: app.weer()

    Button:
        text: "Nothing"
        background_color: 1, 1, 1, 0.6
        background_normal: ""

<ScreenThermo>:
    Button:
        text: "stuff1"
        #this is a test to see if i can switch through screens
        on_press: root.manager.current= 'light'
<ScreenLight>:
    Button:
        text: "stuff2"
<ScreenEnergy>:
    Button:
        text: "stuff3"
<ScreenWeather>:
    Button:
        text: "stuff4"

<Manager>:
    id: screen_manager
    screen_thermo: screen_thermo
    screen_light: screen_light
    screen_energy: screen_energy
    screen_weather: screen_weather

    ScreenThermo:
        id: screen_thermo
        name: 'thermo'
        manager: screen_manager
    ScreenLight:
        id: screen_light
        name: 'light'
        manager: screen_manager
    ScreenEnergy:
        id: screen_energy
        name: 'energy'
        manager: screen_manager
    ScreenWeather:
        id: screen_weather
        name: 'weather'
        manager: screen_manager

As you can see, I'm trying to have my screens switch on a actionbar button click, but somehow when i launch it, my actionbar is gone. If anyone could help me with this issue, that would be amazing.


回答1:


you never add the menu to your app also you need to add a manager property in your menu. Try something like this:

in the .py:

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

...
class MenuApp(App):
    def thermostaat(self):
        print("Thermostaat")
    def verlichting(self):
        print("Verlichting")
    def energie(self):
        print("Energie")
    def weer(self):
        print("Het Weer")

...

Notice that I have removed the build method

In your kv add this block of code at the end:

...
BoxLayout: #use a box layout or whatever you want
    orientation: 'vertical'
    Menu:
        size_hint_y: .1
        manager: manager
    Manager:
        size_hint_y: .9
        id: manager



回答2:


In the following example we will replace the root widget, Manager with Menu, the detail solution is as follow:

main.py - Python Script

1. build method

Make the class Menu the root widget by replacing:

def build(self):
    return Manager()

with:

def build(self):
    return Menu()

2. Menu classs

Declare a variable, manager of type ObjectProperty which we will hook it up to the ScreenManager.

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

menu.kv - kv File

3. Hook up the ObjectProperty to id

Here we hook up the ObjectProperty, manager to the ScreenManager's id, screen_manager so that we can reference it e.g. root.manager.current.

<Menu>:
    manager: screen_manager

4. Define the height of ActionBar

We set the height of the ActionBar to 10% (0.1) of the parent's height using size_hint_y = 0.1

ActionBar:
    size_hint_y: 0.1
    ActionView:

5. Define the height of the Button

We set the height of the Button to 10% (0.1) of the parent's height using size_hint_y = 0.1

    Button:
        size_hint_y: 0.1
        text: "Nothing"

6. Add ScreenManager as children of Menu

We add Manager as children of Menu after Button. Since we did not specify the height, it defaults to the remaining available height i.e. 0.8 (80% of parent's height).

Manager:
    id: screen_manager

Example

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout


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


class ScreenThermo(Screen):
    pass


class ScreenLight(Screen):
    pass


class ScreenEnergy(Screen):
    pass


class ScreenWeather(Screen):
    pass


class Manager(ScreenManager):
    screen_thermo = ObjectProperty(None)
    screen_light = ObjectProperty(None)
    screen_energy = ObjectProperty(None)
    screen_weather = ObjectProperty(None)


class MenuApp(App):
    def thermostaat(self):
        print("Thermostaat")

    def verlichting(self):
        print("Verlichting")

    def energie(self):
        print("Energie")

    def weer(self):
        print("Het Weer")

    def build(self):
        return Menu()


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

menu.kv

#:kivy 1.10.0

<Menu>:
    manager: screen_manager
    orientation: "vertical"
    ActionBar:
        size_hint_y: 0.1
        ActionView:
            ActionPrevious:
            ActionButton:
                text: "Thermostaat"

                on_release: app.thermostaat()
            ActionButton:
                text: "Verlichting"
                #I want my screens to switch when clicking on this actionbar button
                on_press: root.manager.current= 'light'
                on_release: app.verlichting()
            ActionButton:
                text: "Energieverbruik"
                on_release: app.energie()
            ActionButton:
                text: "Het Weer"
                on_release: app.weer()

    Button:
        size_hint_y: 0.1
        text: "Nothing"
        background_color: 1, 1, 1, 0.6
        background_normal: ""
    Manager:
        id: screen_manager

<ScreenThermo>:
    Button:
        text: "stuff1"
        #this is a test to see if i can switch through screens
        on_press: root.manager.current= 'light'
<ScreenLight>:
    Button:
        text: "stuff2"
<ScreenEnergy>:
    Button:
        text: "stuff3"
<ScreenWeather>:
    Button:
        text: "stuff4"

<Manager>:
    id: screen_manager
    screen_thermo: screen_thermo
    screen_light: screen_light
    screen_energy: screen_energy
    screen_weather: screen_weather

    ScreenThermo:
        id: screen_thermo
        name: 'thermo'
        manager: screen_manager
    ScreenLight:
        id: screen_light
        name: 'light'
        manager: screen_manager
    ScreenEnergy:
        id: screen_energy
        name: 'energy'
        manager: screen_manager
    ScreenWeather:
        id: screen_weather
        name: 'weather'
        manager: screen_manager

Output



来源:https://stackoverflow.com/questions/47481919/why-is-my-kivy-actionbar-gone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!