Skip to content

Instantly share code, notes, and snippets.

@antback
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save antback/3d7426d214e63c95e39d to your computer and use it in GitHub Desktop.

Select an option

Save antback/3d7426d214e63c95e39d to your computer and use it in GitHub Desktop.
Kivy custom list item working
#:kivy 1.0
#:set default_font_size "60dp"
#:import rgb kivy.utils.get_color_from_hex
<HeaderLabel@Label>:
font_size: dp(15)
color: 0,0,0,1
<JoinMatchScreen>:
canvas.before:
Color:
rgb: (1, 1, 1)
Rectangle:
size: self.size
pos: self.pos
# orientation: 'vertical'
[ListItem@MyListItemButton]:
size_hint: (1, None)
height: dp(80)
text: str(ctx.id)
BoxLayout:
padding: dp(5)
orientation: 'vertical'
HeaderLabel:
text: str(ctx.created_by)
halign: 'left'
text_size: (self.width, None)
anchor_x: 'left'
anchor_y: 'top'
markup: True
font_size: dp(16)
# separator
BoxLayout:
canvas.before:
Color:
rgb: rgb('#E8EDEC')
Rectangle:
size: (root.width, 1)
pos: (0, self.pos[1])
import kivy
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.textinput import TextInput
from kivy.adapters.listadapter import ListAdapter
from kivy.uix.listview import ListView, SelectableView
from kivy.uix.scrollview import ScrollView
from random import random
from kivy.clock import Clock
import json
from kivy.properties import ObjectProperty, BooleanProperty, NumericProperty
DATA = """
{
"meta": {
"count": 1,
"model": "match",
"next": "",
"page": 1,
"previous": ""
},
"objects": [
{
"id": 1,
"comment": "",
"num_players": 4,
"rated": 1,
"min_rating": 0,
"max_rating": 999,
"challenge_to": null,
"created_by": {
"rating": 1200,
"uid": "me",
"confirmed_at": "11:58:04",
"first_login": "11:58:04",
"id": 1,
"last_login": "11:58:04",
"avatar": null,
"active": true,
"email": "[email protected]"
}
},
{
"id": 2,
"comment": "",
"num_players": 2,
"rated": 1,
"min_rating": 0,
"max_rating": 999,
"challenge_to": null,
"created_by": {
"rating": 1200,
"uid": "you",
"avatar": null,
"active": true,
"email": "[email protected]"
}
}
]
}
"""
class MyListItemButton(BoxLayout, Button, SelectableView):
def __init__(self, **kwargs):
self.background_normal = 'data/img/pixel.png'
super(MyListItemButton, self).__init__(**kwargs)
class ListItem(MyListItemButton, BoxLayout):
def __init__(self, **kwargs):
super(ListItem, self).__init__(**kwargs)
class JoinMatchScreen(BoxLayout):
'''Create a controller that receives a custom widget from the kv lang file.
Add an action to be called from the kv lang file.
'''
def __init__(self, **kwargs):
super(JoinMatchScreen, self).__init__(**kwargs)
o = json.loads(DATA)
self.list_adapter = ListAdapter(
data=o['objects'],
args_converter=self.converter,
selection_mode='single',
allow_empty_selection=True,
template='ListItem')
self.list_adapter.bind(on_selection_change=self.selection_change)
self.list_view = ListView(adapter=self.list_adapter)
self.add_widget(self.list_view)
def selection_change(self, adapter, *args):
if (adapter.selection):
print adapter.selection[0].text
def converter(self, row_index, o):
return {
'created_by': 'created by [b] ' + o['created_by']['uid'] + '[/b]' + '[size=10dp] at 28 August[/size]',
'players': 'Players #' + str(o['num_players']),
'min_rating': '>' + str(o['min_rating']),
'max_rating': '<' + str(o['max_rating']),
'rated': 'Rated',
'id': str(o['id'])
}
class JoinMatchScreenApp(App):
def build(self):
return JoinMatchScreen()
if __name__ == '__main__':
JoinMatchScreenApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment