本文介绍了ListProperty项目(按kv中的索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将高分存储在ListProperty()中,并且我试图通过kv文件(下面的代码)中的索引从该列表中获取一个项目. 它总是说列表索引超出范围".

I have stored highscores in a ListProperty() and I'm trying to fetch an item from that list by index inside kv file (code below). It keeps saying "list index out of range".

.py文件:

scores = ListProperty()

.kv文件:

Label:
    text: str(root.scores[1])

如果我删除[1]索引部分,只是有了str(root.scores),它将完美显示整个列表.

If I remove the [1] index part and just have str(root.scores) it perfectly shows the entire list.

推荐答案

我刚刚找到了答案.创建新的ListProperty()时,我必须知道列表的大小,以便能够使用索引列表访问kv中的项目.因此,当我将ListPropert创建为:

I just found an answer I think. When greating a new ListProperty() I have to know how big the list is in order to be able to access the items in kv using list indexed.So when I created the ListPropert as:

scores = ListProperty([['', 0], ['', 0], ['', 0], ['', 0]])

然后在kv文件中完美运行.

Then it worked perfectly in kv file.

我认为这与所有kivy属性一起使用,因此您必须预先知道属性的确切大小.

I assume this goes with all the kivy properties, so that you have to know exact size of the property prehand.

这篇关于ListProperty项目(按kv中的索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 03:51