当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How can I create a custom listview item in kivy?

问题描述:

I'm trying to to create a custom listview item for a listview in python 3.3 using kivy.

A listitem should consist of a label and 3 buttons.

I created a listview with the adapter cls set to a custom item class as follows:

 ListView:

id: cue_list

adapter: ListAdapter(data=[], cls=ListItemButton)

item_strings: []

I then set class rules like this:

<ListItemButton>:

size_hint_y: root.size_hint_y

height: root.height

orientation:"horizontal"

Button:

size_hint: 0.2, 1.0

text: "delete"

then in my python file I create a function to add some items:

 def addcue(self):

self.lv.adapter.data.extend(["new item"])

self.lv._trigger_reset_populate()

But strangely, the result is this:

http://i.stack.imgur.com/dNmz1.png

How can I specify a listview item from scratch in kivy, using the .kv file language?

网友答案:

Your widgets have the properties and styling of ListItemButton because...well, that's how inheritance works. If you don't want this, create your own list item widget subclassing http://kivy.org/docs/api-kivy.uix.listview.html#kivy.uix.listview.SelectableView. For instance, ListItemButton is a subclass of SelectableView and Button.

Also, all your delete buttons overlap because the ListItemButton is not a layout and so does nothing with the size_hint information you give, therefore all the buttons have the default pos of 0,0 and the default size of 100,100. If you want layout behaviour, make your list item class a layout, e.g. a subclass of BoxLayout and SelectableView.

分享给朋友:
您可能感兴趣的文章:
随机阅读: