基维使用未在屏幕中定义的按钮切换屏幕

时间:2019-04-23 03:55:49

标签: kivy

我的应用存在一些问题。在下面,您将找到许多简化且有效的代码。问题是,当我以这种方式切换屏幕时,用于文本输入或按钮的方法的调用次数与使用该类的次数相同。是否可以像在Screens中那样使用Button中未定义的Screen<List_or_NotesScreen>之间切换?

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen

kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import hex kivy.utils.get_color_from_hex
#:import Factory kivy.factory.Factory


ScreenManager:
    ListScreen:
    NotesScreen:

<ScreenSwitch>

    orientation: 'horizontal'
    size_hint_y: None
    height: 30
    Button:
        text: 'List'
        on_press: app.root.current = 'ListScreen'
    Button:
        text: 'Notes'
        on_press: app.root.current = 'NotesScreen'

<List>:
    orientation: 'vertical'
    Label:
        text: '1st'
    Label:
        text: '2nd'
    Label:
        text: '3rd'
<Notes>:
    TextInput:

<ListScreen>:
    name: 'ListScreen'
    BoxLayout:
        orientation: 'vertical'
        ScreenSwitch:
        List:

<NotesScreen>:
    name: 'NotesScreen'
    BoxLayout:
        orientation: 'vertical'
        ScreenSwitch:
        Notes:


"""

class ScreenSwitch(BoxLayout):
    pass

class List(BoxLayout):
    pass

class Notes(BoxLayout):
    pass

class NotesScreen(Screen):
    pass

class ListScreen(Screen):
    pass



sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

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

我需要类似的内容,但要避免在<List_or_Notes>

中创建按钮
<List_or_NotesScreen>:

BoxLayout:
    orientation: 'vertical'
    ScreenSwitch:
    ScreenManager:
        id: list_or_notes
        Screen:
            name: 'Notes'
            Notes:
        Screen:
            name: 'List'
            List:
'''

2 个答案:

答案 0 :(得分:1)

您可以将ScreenManager放在与按钮相同的BoxLayout内。
检查评论。

from kivy.config import Config

Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen

kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import hex kivy.utils.get_color_from_hex
#:import Factory kivy.factory.Factory

# ScreenManager:  # don't need this here
#     ListScreen:
#     NotesScreen:

ScreenSwitch:  # make this the root widget
    orientation: 'vertical'
    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: 30
        Button:
            text: 'List'
            on_press: screen_man.current = 'ListScreen'
        Button:
            text: 'Notes'
            on_press: screen_man.current = 'NotesScreen'
    ScreenManager:  # add the Screens here
        id: screen_man  # you need this to change screens with buttons
        ListScreen:
        NotesScreen:

<ListA>:
    orientation: 'vertical'
    Label:
        text: '1st'
    Label:
        text: '2nd'
    Label:
        text: '3rd'

<Notes>:
    TextInput:

<ListScreen>:
    name: 'ListScreen'
    BoxLayout:
        orientation: 'vertical'
        # ScreenSwitch:  # remove from here
        ListA:

<NotesScreen>:
    name: 'NotesScreen'
    BoxLayout:
        orientation: 'vertical'
        # ScreenSwitch:  # remove from here
        Notes:
"""


class ScreenSwitch(BoxLayout):
    pass


class ListA(BoxLayout):
    pass


class Notes(BoxLayout):
    pass


class NotesScreen(Screen):
    pass


class ListScreen(Screen):
    pass


sm = Builder.load_string(kv)


class TestApp(App):
    def build(self):
        return sm


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

答案 1 :(得分:0)

如果有人感兴趣,我认为以下代码是一种解决方案:

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen

kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import hex kivy.utils.get_color_from_hex
#:import Factory kivy.factory.Factory


ScreenManager:
    List_or_NotesScreen:

<ScreenSwitch>

    orientation: 'horizontal'
    size_hint_y: None
    height: 30
    Button:
        text: 'List'
        on_press: app.get_running_app().root.get_screen('List_or_NotesScreen').ids.list_or_notes.current = 'List'
    Button:
        text: 'Notes'
        on_press: app.get_running_app().root.get_screen('List_or_NotesScreen').ids.list_or_notes.current = 'Notes'

<List>:
    orientation: 'vertical'
    Label:
        text: '1st'
    Label:
        text: '2nd'
    Label:
        text: '3rd'
<Notes>:
    TextInput:

<List_or_NotesScreen>:
    name: 'List_or_NotesScreen'
    BoxLayout:
        orientation: 'vertical'
        ScreenSwitch:
        ScreenManager:
            id: list_or_notes
            Screen:
                name: 'Notes'
                Notes:
            Screen:
                name: 'List'
                List:


"""

class ScreenSwitch(BoxLayout):
    pass

class List(BoxLayout):
    pass

class Notes(BoxLayout):
    pass



class List_or_NotesScreen(Screen):
    pass



sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

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