.kv和python代码中的变量不同步



当我试图从python代码中访问id为total_area的TextInput时,我会得到以下错误: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
@classmethod
def button_OnPress(self):
print(self.total_area.text)

你写了@classmethod,所以你得到了一个类方法,它的第一个参数不是类的实例(通常称为self(,而是类本身(通常也称为cls(。

因此,您的self.total_area等价于RootWidget.total_area,正如错误所说,它是一个ObjectProperty。

您需要访问total_area作为类实例的属性。在这种情况下,请卸下@classmethod

最新更新