简体   繁体   中英

Why popup dismiss method causes an error? (python kivymd)

I'm trying to make a app. On button click on the bottom right of the screen there appears a dialog window(popup). On "Done" click the popup window is expected to close ( close_dialog method), but for some reason there appears AttributeError:

AttributeError: 'grudget4App' object has no attribute 'close_dialog'

Could you please tell me why the code doesn't work and how to fix it. Thanks.

.py code:

from kivy.lang import Builder
from kivy.core.window import Window
from kivymd.app import MDApp
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scrollview import ScrollView
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.list import TwoLineAvatarListItem

Window.size = (288, 511)

class DialogContent(BoxLayout):
    pass

class Container(Screen):
    dialog = None

    def show_dialog(self, *args):
        '''
        Create group creation popup
        '''
        if not self.dialog:
            self.dialog = MDDialog(
                title="Create new group",
                type="custom",
                content_cls=DialogContent(),
                auto_dismiss=False
            )
        self.dialog.open()

    def close_dialog(self, *args):
        '''
        Close popup on Done click
        '''
        self.dialog.dismiss()

class grudget4App(MDApp):
    def build(self):
        container = Container()
        return container

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

.kv code:

<DialogContent>:
    textfield: textfield
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "120dp"

    MDTextField:
        id: textfield
        hint_text: "Group name"

    MDFlatButton:
        id: btn1
        text: "Done"
        text_color: self.theme_cls.primary_color
        on_release: app.close_dialog()


<Container>:
    MDFloatingActionButton:
        pos_hint: {'right': 0.95, 'y': 0.05}
        icon: "icon.png"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        on_release:
            root.show_dialog()

    Screen:
        NavigationLayout:
            ScreenManager:
                Screen:
                    BoxLayout:
                        orientation: 'vertical'
                        MDToolbar:
                            title: 'Demo Application'
                            left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]
                            elevation:10
                        Widget:
            MDNavigationDrawer:
                id: nav_drawer

The method on_release: app.close_dialog() refers to your grudget4App class, but you want to call the method inside your Container class.

So we need to get the right screen inside your app. Since Container is the only screen there, and also the root, you can use:

on_release: app.root.close_dialog()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM