Переменные в .kv и в коде Python не синхронизируются - PullRequest
1 голос
/ 02 ноября 2019

Когда я пытаюсь получить доступ к TextInput с идентификатором total_area из моего кода Python, я получаю эту ошибку: AttributeError: 'kivy.properties.ObjectProperty' объект не имеет атрибута 'text'. Я использовал отладчик, чтобы увидеть, что не так, и мне кажется, что переменные в коде Python не связываются с переменными в файле kv. Справочный код Python:

class RootWidget(Widget):

    Config.set('graphics', 'resizable', '0')
    Config.set('graphics', 'height', '720')
    Config.set('graphics', 'width', '480')

    koatuu_obl_cent = ObjectProperty()
    koatuu_city = ObjectProperty()
    kitchen_area = ObjectProperty()
    qt_room = ObjectProperty()
    floor = ObjectProperty()
    qt_floor = ObjectProperty()
    total_area = ObjectProperty()
    living_area = ObjectProperty()
    year_building = ObjectProperty()

    @classmethod
    def button_OnPress(self):
        print(self.total_area.text)


class DeepEval(App):

    def build(self):
        return RootWidget()


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

.kv:

#:import Factory kivy.factory.Factory

<MyPopup@Popup>:
    auto_dismiss: False

    size_hint: None, None
    size: 400, 400

<RootWidget>

    koatuu_obl_cent: koatuu_obl_cent
    koatuu_city: koatuu_city
    kitchen_area: kitchen_area
    qt_room: qt_room
    floor: floor
    qt_floor: qt_floor
    total_area: total_area
    living_area: living_area
    year_building: year_building

    Image:
        source: 'imgs/background.jpg'
        size: self.texture_size

    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        size: root.width, root.height

        BoxLayout:
            orientation: 'vertical'
            height: 350
            width: 225
            size_hint_x: None
            size_hint_y: None
            spacing: 22.5

            TextInput:
                id: total_area
                multiline: False
                hint_text: "Param12"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            TextInput:
                id: kitchen_area
                multiline: False
                hint_text: "Param1"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            TextInput:
                id: living_area
                multiline: False
                hint_text: "Param1"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            TextInput:
                id: qt_room
                multiline: False
                hint_text: "Param1"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            TextInput:
                id: floor
                multiline: False
                hint_text: "Param1"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            TextInput:
                id: qt_floor
                multiline: False
                hint_text: "Param1"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            TextInput:
                id: year_building
                multiline: False
                hint_text: "Param1"
                halign: 'center'
                foreground_color: .5, .5, .5, .75
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            Button:
                id: koatuu_obl_cent
                text: 'Open popup'
                on_release: Factory.MyPopup().open()
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

            Button:
                id: koatuu_city
                text: 'Open popup'
                on_release: Factory.MyPopup().open()
                height: 33
                width: 225
                size_hint_x: None
                size_hint_y: None

        AnchorLayout:
            anchor_x: "center"
            anchor_y: "bottom"
            padding: 0,0,0,25

            Button:
                text: "Calculate"
                size_hint_x: None
                size_hint_y: None
                height: 50
                width: 135
                on_press: root.button_OnPress()

        AnchorLayout:
            anchor_x: "right"
            anchor_y: "bottom"

            Button:
                text: "cam"
                size_hint_x: None
                size_hint_y: None
                height: 45
                width: 45

1 Ответ

1 голос
/ 02 ноября 2019
@classmethod
def button_OnPress(self):
    print(self.total_area.text)

Вы написали @classmethod, чтобы вы получили метод класса, первым аргументом которого является не экземпляр класса (обычно называемый self), а сам класс (обычно называемый cls).

Таким образом, self.total_area эквивалентно RootWidget.total_area, что, как говорит ошибка, ObjectProperty.

Вам необходимо получить доступ к total_area как атрибуту экземпляра вашего класса. В этом случае удалите @classmethod.

...