Kivy ScreenManager和BoxLayout

时间:2018-08-07 13:11:24

标签: python kivy

我正处于Kivy的学习过程中,并举了一个简单的例子。应该有两个屏幕。每个屏幕都应该有一个菜单栏。菜单栏显示在两个屏幕的底部。这很完美。现在,我尝试将菜单栏放在屏幕顶部,并且不要让我的示例运行。

这是我的代码片段,可以正常工作:

<ScreenOverview>:
    name:   "ScreenOverview"

    BoxLayout:
        orientation: "vertical"

        Button:
            text: 'Overview Screen'
            font_size: 50

        MenuBar:

当我用按钮切换菜单栏时,我只会在底部看到按钮:

<ScreenOverview>:
    name:   "ScreenOverview"

    BoxLayout:
        orientation: "vertical"

        MenuBar:

        Button:
            text: 'Overview Screen'
            font_size: 50

仅使用MenuBar,我可以看到MenuBar,但也可以在底部而不是顶部看到。

<ScreenOverview>:
    name:   "ScreenOverview"

    BoxLayout:
        orientation: "vertical"

        MenuBar:

我在这里搜索并尝试了许多提示和示例,但是我的代码无法正常工作。有人可以解释我的错觉吗?

这是我完整的py代码:

import kivy
kivy.require('1.10.1') # replace with your current kivy version !

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition

class MenuBar( Button ):
    pass

class ScreenOverview( Screen ):
    def __init__( self, **kwargs ):
        super( ScreenOverview, self ).__init__( **kwargs )

    def switchToScreen( self ):
        if( self.manager.current == "ScreenBookings" ):
            self.manager.transition = SlideTransition( direction="left" )
            self.manager.current = "ScreenOverview"

class ScreenBookings( Screen ):
    def __init__( self, **kwargs ):
        super( ScreenBookings, self ).__init__( **kwargs )

    def switchToScreen( self ):
        if( self.manager.current == "ScreenOverview" ):
            self.manager.transition = SlideTransition( direction="right" )
            self.manager.current = "ScreenBookings"

class RVScreenManager( ScreenManager ):
    pass

class RVApp( App ):
    def build( self ):
        self.ScreenManager = RVScreenManager()

        self.ScreenOverview = ScreenOverview( name="ScreenOverview")
        self.ScreenManager.add_widget( self.ScreenOverview )

        self.ScreenBookings = ScreenBookings( name="ScreenBookings")
        self.ScreenManager.add_widget( self.ScreenBookings )

        return( self.ScreenManager )

    def switchScreenOverview( self ):
        self.ScreenOverview.switchToScreen()

    def switchScreenBookings( self ):
        self.ScreenBookings.switchToScreen()

Builder.load_file( "sf-fragen01.kv" )

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

和kv文件:

#:import SlideTransition kivy.uix.screenmanager.SlideTransition

<RVScreenManager>:
    id: ScreenManager

    ScreenOverview:
        name: "ScreenOverview"
        screen_manager: "ScreenManager"
    ScreenBookings:
        name: "ScreenBookings"
        screen_manager: "ScreenManager"

<ScreenOverview>:
    name:   "ScreenOverview"

    BoxLayout:
        orientation: "vertical"

        Button:
            text: 'Overview Screen'
            font_size: 50

        MenuBar:

<ScreenBookings>:
    name: "ScreenBookings"

    BoxLayout:
        orientation: "vertical"

        Button:
            text: 'Bookings Screen'
            font_size: 50

        MenuBar:

<MenuButton@Button>:
    font_size:      20
    text_size:      self.size
    halign:         "center"
    valign:         "center"

<MenuBar>:
    name: "MenuBar"

    BoxLayout:
        size:       root.size[0], root.size[1]

        MenuButton:
            on_release:
                app.switchScreenOverview()
            text: 'Overview'

        MenuButton:
            on_release:
                app.switchScreenBookings()
            text: 'Bookings'

1 个答案:

答案 0 :(得分:0)

我认为这可能是由于您的MenuBar扩展了ButtonMenuBar扩展某种类型的Layout更有意义。这是您的py代码,其中MenuBar扩展了BoxLayout

import kivy
from kivy.uix.boxlayout import BoxLayout

kivy.require('1.10.1') # replace with your current kivy version !

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition

class MenuBar( BoxLayout ):
    pass

class ScreenOverview( Screen ):
    def __init__( self, **kwargs ):
        super( ScreenOverview, self ).__init__( **kwargs )

    def switchToScreen( self ):
        if( self.manager.current == "ScreenBookings" ):
            self.manager.transition = SlideTransition( direction="left" )
            self.manager.current = "ScreenOverview"

class ScreenBookings( Screen ):
    def __init__( self, **kwargs ):
        super( ScreenBookings, self ).__init__( **kwargs )

    def switchToScreen( self ):
        if( self.manager.current == "ScreenOverview" ):
            self.manager.transition = SlideTransition( direction="right" )
            self.manager.current = "ScreenBookings"

class RVScreenManager( ScreenManager ):
    pass

class RVApp( App ):
    def build( self ):
        self.ScreenManager = RVScreenManager()

        self.ScreenOverview = ScreenOverview( name="ScreenOverview")
        self.ScreenManager.add_widget( self.ScreenOverview )

        self.ScreenBookings = ScreenBookings( name="ScreenBookings")
        self.ScreenManager.add_widget( self.ScreenBookings )

        return( self.ScreenManager )

    def switchScreenOverview( self ):
        self.ScreenOverview.switchToScreen()

    def switchScreenBookings( self ):
        self.ScreenBookings.switchToScreen()

Builder.load_file( "sf-fragen01.kv" )

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

还有.kv文件,其顶部是MenuBar。请注意,MenuBar不再包含BoxLayout,因为MenuBarBoxLayout

#:import SlideTransition kivy.uix.screenmanager.SlideTransition

<RVScreenManager>:
    id: ScreenManager

    ScreenOverview:
        name: "ScreenOverview"
        screen_manager: "ScreenManager"
    ScreenBookings:
        name: "ScreenBookings"
        screen_manager: "ScreenManager"

<ScreenOverview>:
    name:   "ScreenOverview"

    BoxLayout:
        orientation: "vertical"

        MenuBar:

        Button:
            text: 'Overview Screen'
            font_size: 50

<ScreenBookings>:
    name: "ScreenBookings"

    BoxLayout:
        orientation: "vertical"

        MenuBar:

        Button:
            text: 'Bookings Screen'
            font_size: 50

<MenuButton@Button>:
    font_size:      20
    text_size:      self.size
    halign:         "center"
    valign:         "center"

<MenuBar>:
    name: "MenuBar"

    size:       root.size[0], root.size[1]

    MenuButton:
        on_release:
            app.switchScreenOverview()
        text: 'Overview'

    MenuButton:
        on_release:
            app.switchScreenBookings()
        text: 'Bookings'