Kivy Popup无法访问root方法

时间:2018-06-02 04:52:43

标签: methods popup kivy access

我是新手,如果我没有遵循正确的程序,请随时纠正我。

我有一个打开弹出窗口的Kivy应用程序。在弹出窗口中,我可以输入2个数字然后单击添加按钮,该按钮应该添加2个数字。我收到一条错误消息,“AttributeError:'CustomPopup'对象没有属性'addNum'”

为什么会这样?

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文件

#: 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()

2 个答案:

答案 0 :(得分:0)

首先,要访问addNum,您必须从kv部分调用app.root.addNum。 您还必须发送要添加的值,即您在文本框中输入的文本:(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()

答案 1 :(得分:0)

如果您在该类的某个地方有参考,则可以从MyStuff访问这些方法 执行root.addNum()后,您尝试访问CustomPopup内的方法,该方法是根本情况 因此,在这种情况下,我要做的是将MyStuff定义为App类的属性,(self.ms = MyStuff())。这样,您可以通过执行kvapp.ms.addNum()访问addNum 您还需要将数字传递给class MyStuff(BoxLayout): answer = '' def openPopup(self): the_popup = CustomPopup() the_popup.open() def addNum(self, num1, num2): self.answer = str(int(num1) + int(num2)) class MyStuffApp(App): def build(self): self.ms = MyStuff() return self.ms

在py。:

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

在kv。:

{{Fruit|Banana}}