切换screenmanager里面的布局

时间:2016-12-26 23:10:37

标签: python python-2.7 kivy kivy-language

我试图在BoxLayout中创建一个ScreenManager,所以我可以在屏幕的每个屏幕下面都有一个固定的工具箱。我设法显示第一个屏幕(感谢this question),但是,当我尝试切换到另一个屏幕时,应用程序崩溃说没有其他屏幕。

实际上,确实没有其他屏幕:ScreenManagement内的两个版画 init 都没有显示任何内容。而且我不知道为什么。

没有工具栏(当然只有ScreeManager(代码)和代码中的必要调整)一切正常。

我尝试将add_widget添加到ScreenManagement并填充了screen_names,但我无法在屏幕之间切换。

我缺少什么?

错误的最后一部分:

screen = self.get_screen(value)
   File "C:\Python27\lib\site-packages\kivy\uix\screenmanager.py", line 944, in get_screen
     raise ScreenManagerException('No Screen with name "%s".' % name)
 ScreenManagerException: No Screen with name "init".

Windows 7,Python 2.7,Kivy 1.9.1

这是ClassApp.py:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock

#just solving my weak GPU issue
from kivy import Config
Config.set('graphics', 'multisamples', '0')

kivy.require('1.9.1')

class Init(Screen):
    pass

class Menu(Screen):
    pass

class ScreenManagement(ScreenManager):

    def __init__(self,**kwargs):
        print('before super: ', self.screen_names)
        super(ScreenManagement,self).__init__(**kwargs)
        print('after super: ', self.screen_names)

    def switch_to_menu(self):
        self.current = 'menu'
        print('going to menu')

    def switch_to_init(self):
        self.current = 'init'
        print('going to init')

class ClassAllScreen(BoxLayout):
    sm = ScreenManagement()
    sm.transition = NoTransition()
    pass

class ClassApp(App):

    def build(self):
        self.root = ClassAllScreen()
        return self.root

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

这里是Class.kv:

<Init>: #first Screen
    name: 'init'
    BoxLayout:
        orientation:'vertical'
        padding:20
        spacing:10
        Button:
            text:'uppest'
        GridLayout:
            spacing:10
            cols:2

            Button:
                text:'upper left'
            Button:
                text:'upper right'
            Button:
                text:'bottom left'
            Button:
                text:'bottom right'
        Button:
            text:'bottomest: go to menu'
            on_press:app.root.sm.switch_to_menu()       

<Menu>: #second Screen
    name: 'menu'

    BoxLayout:
        orientation:'vertical'
        padding:20
        spacing:10
        GridLayout:
            spacing:10
            cols:2

            Button:
                text:'upper left'
            Button:
                text:'upper right'
            Button:
                text:'bottom left'
            Button:
                text:'bottom right'
        Button:
            text:'bottomy'
        Button:
            text:'bottomest: go to init'
            on_press:app.root.sm.switch_to_init()

<ScreenManagement>: #including both the Screens in ScreenManager
    Menu:
    Init:

<ClassAllScreen>: #all the display with ScreenManager and "Toolbar"
    orientation:'vertical'
    ScreenManagement:
    BoxLayout:
        size_hint_y: None
        height: 60
        spacing: 5
        padding: 5,5,0,5

        canvas:
            Color:
                rgba: .1,.1,.1,1
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            text:'1'
            size_hint_x: None
            width: 60
        Button:
            text:'2'
            size_hint_x: None
            width: 60
        Button:
            text:'3'
            size_hint_x: None
            width: 60

2 个答案:

答案 0 :(得分:1)

首先,您需要将屏幕添加到屏幕管理器中 为此,您可以在ScreenManager类中执行类似的操作。

def __init__(self,**kwargs):
    super(ScreenManagement,self).__init__(**kwargs)
    self.add_widget(Init(name="init"))
    self.add_widget(Menu(name="menu"))

现在两个屏幕将有一个管理员,即ScreenManagement
所以在你的kv代码中你可以这样称呼经理:

Button:
    text:'bottomest: go to menu'
    on_press:root.manager.switch_to_menu()  

如果您不想制作方法,也可以这样做:

Button:
    text:'bottomest: go to menu'
    on_press:root.manager.current = 'menu'

您的代码也存在问题,您将screenmanager添加到boxlayout,并尝试返回boxlayout。你不能这样做。您一次只能显示一个屏幕。所以我猜你想要第三个屏幕。您应该做的是在构建方法中重新调整屏幕管理器,并仅向其添加屏幕 我写了一个例子。我不知道你想要的是这样的东西。

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


Builder.load_string("""

<Init>:
    name: 'init'
    BoxLayout:
        orientation:'vertical'
        Button:
            text:'go to menu'
            on_press:root.manager.current = "menu"


<Menu>:
    name: 'menu'

    BoxLayout:
        orientation:'vertical'
        Button:
            text:'go to init'
            on_press:root.manager.current = "init"


<ClassAllScreen>:
    BoxLayout:
        orientation:'vertical'
        Button:
            text:'go to init'
            on_press:root.manager.current = "init"
        Button:
            text:'go to menu'
            on_press:root.manager.current = "menu"

""")


class Init(Screen):
    pass

class Menu(Screen):
    pass

class ClassAllScreen(Screen):
    pass


class ClassApp(App):

    def build(self):
        sm = ScreenManager()
        sm.add_widget(ClassAllScreen(name="main"))
        sm.add_widget(Init(name="init"))
        sm.add_widget(Menu(name="menu"))
        return sm


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

答案 1 :(得分:0)

在@ EL3PHANTEN和this的帮助下,我修复了我的代码,我想我知道我的错误在哪里。

我无法在kv文件中使用app.root.sm.current='whateverScreenName'。我无法解释原因,但我想它与ScreenManagement的实例有关。正如我从头开始想的那样,python部分非常简单。

再次:感谢您的帮助,@ EL3PHANTEN:)

结果如下:

ScreenManager inside a BoxLayout to have a Toolbar

这是工作的固定python代码:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock

#just solving my weak GPU issue
from kivy import Config
Config.set('graphics', 'multisamples', '0')

kivy.require('1.9.1')

class ScreenManagement(ScreenManager):
    pass

class Init(Screen):
    pass

class Menu(Screen):
    pass


class ClassAllScreen(BoxLayout):
    pass

class ClassApp(App):

    def build(self):
        self.root = ClassAllScreen()
        return self.root

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

和Class.kv:

#: import NoTransition kivy.uix.screenmanager.NoTransition
<Init>:
    name: 'init'
    BoxLayout:
        orientation:'vertical'
        padding:20
        spacing:10
        Button:
            text:'uppest'
        GridLayout:
            spacing:10
            cols:2

            Button:
                text:'upper left'
            Button:
                text:'upper right'
            Button:
                text:'bottom left'
            Button:
                text:'bottom right'
        Button:
            text:'bottomest: go to menu'
            on_press: root.manager.current = 'menu'

<Menu>:
    name: 'menu'     
    BoxLayout:
        orientation:'vertical'
        padding:20
        spacing:10
        GridLayout:
            spacing:10
            cols:2

            Button:
                text:'upper left'
            Button:
                text:'upper right'
            Button:
                text:'bottom left'
            Button:
                text:'bottom right'
        Button:
            text:'bottomy'
        Button:
            text:'bottomest: go to init'
            on_press: root.manager.current = 'init'

<ScreenManagement>:
    transition: NoTransition()
    Init:
    Menu:

<ClassAllScreen>:
    orientation:'vertical'
    ScreenManagement:
    BoxLayout:
        size_hint_y: None
        height: 60
        spacing: 5
        padding: 5,5,0,5

        canvas:
            Color:
                rgba: .1,.1,.1,1
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            text:'1'
            size_hint_x: None
            width: 60
        Button:
            text:'2'
            size_hint_x: None
            width: 60
        Button:
            text:'3'
            size_hint_x: None
            width: 60
相关问题