如何在Python Kivy中更改屏幕后关闭ModalView

时间:2019-02-19 09:52:42

标签: kivy

我有ModelView类和Screen类,当打开Modal View并在更改为第二个Screen Class后在第一个Screen类上做了一些事情,但是模态解散不起作用

/ İmportModuls /

class SView(ScrollView):

    def __init__(self, **kwargs):
        super(SView, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1)
        self.MM = MyModal()
        self.MM.MyButton=self.MyButton

    def MyButton(self, *args):
        print("Hello")

    def MMDismiss(self, *args):
        pass

    def ModalOpen(self, instance, button, *args):
        self.MM.open()
        self.MM.MyButton = self.MyButton
        self.MMDismiss = self.MM.dismiss


    def update(self, *args):

        layout = GridLayout(width=480, padding=10, cols=7, spacing=10, size_hint_y=None)

        layout.bind(minimum_height=layout.setter('height'))

        self.buttons=["One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three"]


        for key in self.buttons:

            stacklayout = StackLayout(size_hint_y=None, size_hint_x=None, height=130, width=100, orientation='lr-tb')

            btn = Button(text=key, size_hint_y=None, size_hint_x=None, height=130, width=100)

            stacklayout.add_widget(btn)
            btn.bind(on_release=partial(self.ModalOpen, key))

            layout.add_widget(stacklayout)
        self.clear_widgets()
        self.add_widget(layout)

/ *“我的滚动视图”,用于在屏幕1上放置按钮* /

class MyModal(ModalView):

    text = StringProperty("text")

    def __init__(self, **kwargs):
        super(MyModal, self).__init__(**kwargs)
        self.size_hint = (None, None)
        self.size = (200, 200)
        self.layout=GridLayout(cols=1,padding=10,spacing=10)
        self.layout.add_widget(Label(text=self.text))
        self.btn=Button(text="My Button")
        self.bind(on_press=self.MyButton)
        self.layout.add_widget(self.btn)
        self.add_widget(self.layout)

    def MyButton(self, *args):
       pass

/ *当按屏幕1的按钮时,打开模态* /

class Screen1(Screen):
    def __init__(self, **kwargs):
        super(Screen1, self).__init__(**kwargs)
        self.sview=SView()
        self.mylayout = GridLayout(cols=1)
        self.mylayout.add_widget(self.sview)
        self.add_widget(self.mylayout)
        self.sview.MyButton = self.MyButton

    def MyButton(self, instance, button):
        self.manager.current = "screen2"
        print("screen 2")


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

/ 我的屏幕1 /

Builder.load_string('''
<SM>:
    Screen1:
        name: "screen1"
    Screen2:
        name: "screen2"

''')

/ 我的构建器字符串 /

class SM(ScreenManager):
    def __init__(self, **kwargs):
        super(SM, self).__init__(**kwargs)
        self.current = 'screen1'

/ 我的屏幕管理器 /

class Main(App):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)


    def build(self):
        self.title = "MAVA"
        self.icon = "./images/logo/logo.png"
        return SM()

if __name__ == "__main__":
    Main().run()

/ 我的应用 /

{{1}}

1 个答案:

答案 0 :(得分:0)

在您的MyModal类中,您可以将dismiss添加到Button中,如下所示:

class MyModal(ModalView):

    text = StringProperty("text")

    def __init__(self, **kwargs):
        super(MyModal, self).__init__(**kwargs)
        self.size_hint = (None, None)
        self.size = (200, 200)
        self.layout=GridLayout(cols=1,padding=10,spacing=10)
        self.layout.add_widget(Label(text=self.text))
        self.btn=Button(text="My Button", on_press=self.dismiss)  # add dismiss action
        self.layout.add_widget(self.btn)
        self.add_widget(self.layout)

我还删除了以下行:self.bind(on_press=self.MyButton)。我认为那没做。