为什么我的Kivy Actionbar不见了? [英] Why is my Kivy Actionbar gone?

查看:106
本文介绍了为什么我的Kivy Actionbar不见了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好

我正在尝试将kivy的动作栏与屏幕管理器结合在一起.我已经可以切换屏幕了,但是无法显示操作栏.我遵循了很多示例,但是没有一个可以帮助我解决问题.我对kivy还是比较陌生,所以我已经很长时间没有使用它了. 我有点想知道是否有人可以指出我的问题所在,因为我正在尝试使用操作栏来构建自己的GUI,该操作栏可以让我在屏幕之间切换. 这是我的main.py:

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()

这是我的menu.kv文件:

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.

推荐答案

您永远不会将菜单添加到应用中,还需要在菜单中添加manager属性.尝试这样的事情:

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

.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

在您的kv末尾添加以下代码块:

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

这篇关于为什么我的Kivy Actionbar不见了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆