为什么我的Kivy Actionbar消失了?

时间:2017-11-25 01:44:50

标签: python user-interface raspberry-pi kivy kivy-language

晚上好,

我试图将kivy的动作栏与屏幕管理员结合起来。我已经达到了可以切换屏幕的程度,但我无法显示我的操作栏。我已经关注了很多例子,但没有一个可以帮助我解决我的问题。我对kivy很新,所以我很久没有和它一起工作了。 我有点想知道是否有人能够指出我的问题所在,因为我试图用一个操作栏来构建我自己的GUI,让我可以切换屏幕。 这是我的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文件:

#: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

正如您所看到的,我试图让我的屏幕在操作栏按钮上单击,但不知何故,当我启动它时,我的操作栏就消失了。 如果有人能帮我解决这个问题,那就太棒了。

2 个答案:

答案 0 :(得分:1)

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

中的.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")

...

请注意,我已删除了构建方法

在你的kv中,最后添加这段代码:

...
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

答案 1 :(得分:1)

在下面的示例中,我们将使用Menu替换根小部件Manager,详细解决方案如下:

main.py - Python脚本

1。构建方法

通过替换:

使类菜单成为根小部件
def build(self):
    return Manager()

使用:

def build(self):
    return Menu()

2。菜单类

声明一个变量,类型为ObjectProperty的管理器,我们将它连接到ScreenManager。

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

menu.kv - kv文件

3。将ObjectProperty连接到id

这里我们将ObjectProperty,manager连接到ScreenManager的id,screen_manager,以便我们可以引用它,例如root.manager.current。

<Menu>:
    manager: screen_manager

4。定义ActionBar的高度

我们使用size_hint_y = 0.1

将ActionBar的高度设置为父级高度的10%(0.1)
ActionBar:
    size_hint_y: 0.1
    ActionView:

5。定义Button的高度

我们将Button的高度设置为父亲身高的10%(0.1) size_hint_y = 0.1

    Button:
        size_hint_y: 0.1
        text: "Nothing"

6。将ScreenManager添加为菜单

的子项

我们在Button之后添加Manager作为Menu的子项。由于我们没有指定高度,因此默认为剩余的可用高度,即0.8(父母高度的80%)。

Manager:
    id: screen_manager

实施例

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

输出

Figure 1 - App Starup Figure 2 - Clicked Verlichting & Screen stuff2 Displayed