无法关闭猕猴桃中的弹出窗口

时间:2019-06-10 21:28:34

标签: python kivy

我正在使用一个简单的应用程序,使用wbdata,matplotlib和kivy显示人均GDP。我想显示一个带有简单消息的弹出窗口,但是单击应该关闭弹出窗口的按钮后出现错误。我不知道我做错了什么。 这是我的代码的一部分:

class P(FloatLayout):
    pass


class MyGrid(Widget):

    country_text_input = ObjectProperty()

#----------------------------------------------------------------------------------------
# Events
    def plot_button_pressed(self):
        country_name = self.country_text_input.text

        try:
            country_code = search_for_country_code(country_name)

            country_data = get_country_data(country_name, country_code)

            country_data.plot_gdp_info()

        except:
            show_popup()


def show_popup():
    show = P()

    popupWindow = Popup(title="Error", content=show, size_hint=(None,None),size=(400,400))

    popupWindow.open()


#----------------------------------------------------------------------------------------
# Building UserInterface (main application)
class MyApp(App):
    def build(self):
        return MyGrid()

.kv文件

#:kivy 1.11.0

<MyGrid>:

    country_text_input: country_text_input

    GridLayout:
        cols: 1
        size: root.width, root.height


        GridLayout:
            cols: 2

            Label:
                text: "Country: "

            TextInput:
                id: country_text_input
                multinline: False


        GridLayout:

            cols: 1

            Button:
                text: "Show GDP per capita data"
                on_press: root.plot_button_pressed()

<P>:

    Label:
        text: "Country not found"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}

    Button:
        text: "OK"
        size_hint: 0.8, 0.2
        pos_hint: {"x":0.1, "y":0.1}
        on_press: root.dismiss()

单击按钮后出现错误 AttributeError:'P'对象没有属性'dismiss'

请帮助

1 个答案:

答案 0 :(得分:0)

root中的kv指的是在其中使用的块的根。请参见documentation。因此,root.dismiss()试图调用您的dismiss()类的P方法(不存在)。我认为您需要保存对Popup的引用,以供以后使用(关闭)。您可以通过修改python代码来做到这一点:

class MyGrid(Widget):

    country_text_input = ObjectProperty()

#----------------------------------------------------------------------------------------
# Events
    def plot_button_pressed(self):
        country_name = self.country_text_input.text

        try:
            country_code = search_for_country_code(country_name)

            country_data = get_country_data(country_name, country_code)

            country_data.plot_gdp_info()

        except:
            self.popup = show_popup() # save reference to the Popup


def show_popup():
    show = P()

    popupWindow = Popup(title="Error", content=show, size_hint=(None,None),size=(400,400))

    popupWindow.open()
    return popupWindow # return a reference to the Popup

,然后在您的kv中,使用该引用关闭Popup

<P>:

    Label:
        text: "Country not found"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}

    Button:
        text: "OK"
        size_hint: 0.8, 0.2
        pos_hint: {"x":0.1, "y":0.1}
        on_press: app.root.popup.dismiss() # uses saved Popup reference
相关问题