我们如何检查 python 的 kivy 库中的按钮中是否存在图像?



所以我遇到的问题是,我在我的。kv文件中放置了一个图像在我的按钮对象内,我想检查图像是否存在于按钮内。现在的问题是,他们都是小部件(一个在另一个),所以我的实例方法不工作。下面是我的代码。

GridLayout:
id: chess_board
cols: 8
rows: 8
padding: ("60dp", "60dp", "60dp", "60dp")
orientation: 'tb-lr'

Button:
id: button_1
background_normal: ''
background_color: (1,1,1,1)
on_press: app.moves(self)
AsyncImage:
id: white_1
source: 'imgs/WhiteCastle.png'
center_x: self.parent.center_x
center_y: self.parent.center_y

现在我想检查AsyncImage是否存在于按钮中因为在我创建的其他按钮中它不存在。有人能帮帮我吗?下面是我的。py文件。

def moves(self, instance):
all_ids = instance.parent.parent.ids.items()
id = self.get_id(instance)
print(id)
def get_id(self, instance):
for id, widget in instance.parent.parent.ids.items():
if widget.__self__ == instance:
return id

如果你稍微改变一下moves()的方法,像这样:

def moves(self, instance, root):
if 'white_1' in root.ids:
asyncImage = root.ids.white_1
if asyncImage in instance.children:
print('Button contains an AsyncImage')
return
print('Button does not contain an AsyncImage')

并更改kv:

on_press: app.moves(self)

:

on_press: app.moves(self, root)