如何使用screenmanager小部件在kivy中添加动作栏?

时间:2019-04-02 20:16:11

标签: python kivy

我正在尝试在项目的第一个屏幕的顶部添加一个操作栏。我尝试使用screenmanager小部件并发送操作栏,因为它就像控制/获取两个屏幕的孩子一样。最初,我尝试在第一个屏幕中将操作条形码添加到root.widget中,但它们将此类显示为无效类。

如何同时添加两者?另外,即使我添加了orientation : 'vertical'

,也无法从上到下显示按钮
    import kivy
    kivy.require('1.10.1')

    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    from kivy.uix.gridlayout import GridLayout

    from kivy.uix.screenmanager import ScreenManager,Screen,FadeTransition

    class SomeLayout_GridLayout(Screen):
        pass
    class FirstScreen(Screen):
        pass
    class SecondScreen(Screen):
        pass
    class ScreenManager(ScreenManager):
        pass

    root_widget = Builder.load_string('''
      ScreenManager:
        FirstScreen:
        SecondScreen:
        SomeLayout_GridLayout:
     <FirstScreen>:
        name: 'first'
        <SomeLayout_GridLayout>:
        cols: 1
        rows: 2
        row_force_default: True
        rows_minimum: {0: ActionBar.height, 1: self.height - ActionBar.height}
        SomeMenu_ActionBar:
            id: ActionBar

        <SomeMenu_ActionBar@ActionBar>:
        ActionView:
            id: ActionView
            ActionGroup:
                id: App_ActionGroup
                mode: 'spinner'
                text: 'App'
                ActionButton:
                    text: 'Settings'
                    on_press: app.open_settings()
                ActionButton:
                    text: 'Quit'
                    on_press: app.get_running_app().stop()

            ActionGroup:
                id: File_ActionGroup
                mode: 'spinner'
                text: 'File'
                ActionButton:
                    text: 'Open'
                ActionButton:
                    text: 'Save'
    <HiddenIcon_ActionPrevious@ActionPrevious>:
        title: app.title if app.title is not None else 'Action Previous'
        with_previous: False
        app_icon: ''
        app_icon_width: 0
        app_icon_height: 0
        size_hint_x: None
        width: len(self.title) * 10

    <HiddenText_ActionPrevious@ActionPrevious>: #
        with_previous: False
        on_press: print(self)
        title: ''

    <Hidden_ActionPrevious@ActionPrevious>:
        with_previous: False
        on_press: print(self)
        title: ''
        size_hint: None, None
        size: 0, 0
        BoxLayout:
            orientation: 'horizontal'
            BoxLayout:
                Button:
                    text: 'Crime Prediction'
                    font_size: 30
                    on_release: app.root.current = 'second'
                Button:
                    text: 'Forum'
                    font_size: 30
                    on_release:  app.root.current = 'second'
                Button:
                    text: 'Probable Suspect'
                    font_size: 30
                    on_release: app.root.current = 'second'
    <SecondScreen>:
        name: 'second'
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: 'Predict Crime Nigga!'
                font_size: 50
            BoxLayout:`enter code here`
                Button:
                    text: 'Back to Main Menu'
                    font_size: 30
                    on_release: app.root.current = 'first'
                Button:
                    text: 'get random colour screen'
                    font_size: 30
                    on_release: app.root.current = 'first'
    ''')
    class ScreenManagerApp(App):
        def build(self):
            return root_widget

   ScreenManagerApp().run()

1 个答案:

答案 0 :(得分:0)

带有ActionBar和ScreenManager的Kivy应用程序

  1. 声明继承为BoxLayout的根窗口小部件
  2. ActionBar添加为根小部件的子代
  3. ScreenManager添加为root小部件的子级,并添加id: sm

摘要

BoxLayout:
    orientation: 'vertical'

    ActionBar:
        ...

    ScreenManager:
        id: sm
        FirstScreen:
        SecondScreen:

示例

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder


class WelcomeScreen(Screen):
    pass


class FirstScreen(Screen):
    pass


class SecondScreen(Screen):
    pass


class ScreenManager(ScreenManager):
    pass


class CrimePrevention(BoxLayout):
    pass


Builder.load_file("main.kv")


class TestApp(App):
    title = 'Kivy ScreenManager & ActionBar Demo'

    def build(self):
        return CrimePrevention()


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

main.kv

#:kivy 1.11.0
#:import sp kivy.metrics.sp
#:import dp kivy.metrics.dp

<CrimePrevention>:
    orientation: 'vertical'

    canvas.before:
        Color:
            rgb: .6, .6, .6
        Rectangle:
            pos: self.pos
            size: self.size
            # source: 'data/background.png'

    SomeMenu_ActionBar:
        id: ActionBar

    ScreenManager:
        id: sm
        WelcomeScreen:
        FirstScreen:
        SecondScreen:

<SomeMenu_ActionBar@ActionBar>:

    ActionView:
        id: ActionView

        HiddenIcon_ActionPrevious:

        ActionGroup:
            id: App_ActionGroup
            mode: 'spinner'
            text: 'Jump to Screen'

            ActionButton:
                text: 'Crime Prediction'
                on_release: app.root.ids.sm.current = 'second'
            ActionButton:
                text: 'Forum'
                on_release:  app.root.ids.sm.current = 'second'
            ActionButton:
                text: 'Probable Suspect'
                on_release:  app.root.ids.sm.current = 'second'

        ActionGroup:
            id: App_ActionGroup
            mode: 'spinner'
            text: 'App'

            ActionButton:
                text: 'Settings'
                on_press: app.open_settings()
            ActionButton:
                text: 'Quit'
                on_press: app.get_running_app().stop()

        ActionGroup:
            id: File_ActionGroup
            mode: 'spinner'
            text: 'File'

            ActionButton:
                text: 'Open'
            ActionButton:
                text: 'Save'

<HiddenIcon_ActionPrevious@ActionPrevious>:
    title: ''   # app.title if app.title is not None else 'Action Previous'
    with_previous: False
    app_icon: ''
    app_icon_width: 0
    app_icon_height: 0
    size_hint_x: None
    width: len(self.title) * 10

<WelcomeScreen>:
    name: 'welcome'
    Label:
        text: 'Welcome Screen'
        font_size: sp(50)

<FirstScreen>:
    name: 'first'
    Label:
        text: 'First Screen'

<SecondScreen>:
    name: 'second'
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Predict Crime'
            font_size: 50

        BoxLayout:
            Button:
                text: 'Back to Main Menu'
                font_size: 30
                on_release: app.root.ids.sm.current = 'first'
            Button:
                text: 'get random colour screen'
                font_size: 30
                on_release: app.root.ids.sm.current = 'first'

输出

Kivy ActionBar & ScreenManager