繁体   English   中英

无法在kivy关闭弹出窗口

[英]Can't close popup in kivy

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

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'

请帮忙

rootkv是指它是在所使用的块的根。见文档 所以root.dismiss()试图调用你的P类的dismiss()方法(它不存在)。 我认为您需要保存对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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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