首页 > 解决方案 > Kivy Popup not able to access root method

问题描述

I am a newbie here so feel free to correct me if I am not following proper procedures.

I have a Kivy app that opens a popup. In the popup, I can enter 2 numbers then click the Add button which should add the 2 numbers. I get an error saying, "AttributeError: 'CustomPopup' object has no attribute 'addNum'"

Why would this be?

test.py file

import kivy
kivy.require('1.9.1') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty

class CustomPopup(Popup):
    pass

class MyStuff(BoxLayout):

    num1 = StringProperty
    num2 = StringProperty
    answer = ''

    def openPopup(self):
        the_popup = CustomPopup()
        the_popup.open()

    def addNum(self):
        self.answer = str(int(self.num1) + int(self.num2))

class MyStuffApp(App):

    def build(self):
        return MyStuff()

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

mystuff.kv file

#: import main test

<MyStuff>:
    orientation: 'vertical'
    spacing: 5
    padding: 5


    Button:
        text: 'Change numbers'
        on_press: root.openPopup()
        font_size: 50

    Label:
        text: root.answer


<CustomPopup>:
    size_hint: .5, .5
    auto_dismiss: False
    title: 'Addition'
    num1: number2
    num2: number2

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: '1st number'

        TextInput:
            id: number1

        Label
            text: '2nd number'

        TextInput
            id: number2

        Button:
            text: 'Add'
            on_press: root.addNum()

标签: methodspopupkivy

解决方案


首先,要访问addNum,您必须app.root.addNum从 kv 部分调用。您还必须发送要添加的值,即您在文本框中输入的文本:(number1.text, number2.text)。所以运行代码可能是这样的:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup

Builder.load_string("""
<MyStuff>:
    orientation: 'vertical'
    spacing: 5
    padding: 5
    Button:
        text: 'Change numbers'
        on_press: root.openPopup()
        font_size: 50

    Label:
        text: root.answer


<CustomPopup>:
    size_hint: .5, .5
    auto_dismiss: False
    title: 'Addition'
    num1: number2
    num2: number2

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: '1st number'

        TextInput:
            id: number1

        Label
            text: '2nd number'

        TextInput
            id: number2

        Button:
            text: 'Add'
            on_press: app.root.addNum(number1.text, number2.text)
""")


class CustomPopup(Popup):
    pass


class MyStuff(BoxLayout):

    # num1 = StringProperty()
    # num2 = StringProperty()
    answer = ''

    def openPopup(self):
        the_popup = CustomPopup()
        the_popup.open()

    def addNum(self, *args):
        # self.answer = str(int(self.num1) + int(self.num2))
        self.answer = str(int(args[0]) + int(args[1]))
        print(self.answer)


class MyStuffApp(App):

    def build(self):
        return MyStuff()


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

推荐阅读