I want to add a dynamic checkbox list in kivy (that I can add, remove, or edit items) that correspond to a python list. I've already read this article Kivy Language Dynamic Checkbox List but no dice there. I'm trying to implement this on the python side with add_widget within a for loop (each item will have its own row, that will go into a gridview with 3 columns, one for a checkbox, one for a label, and the other for another checkbox). But kivy is giving me grief about it. I've also found the listadapter, but there's only a ButtonList within it, I've tried to add my own CheckBoxList but that's proven problematic as well (I don't have the exact errors as I was doing this several days ago). I'm using a screen manager for multiple screens and am using a .kv file. Not sure if you can add_widgets on the python side and have the .kv file at the same time. Thanks in advance.
python file:
class MainScreen(Screen):
food_database = [['salmon', 34, 23, 30, 400], ['another food', 34, 23, 2, 700], ['fat burger', 40, 34, 39, 478]]
class CustomFoodListView():
def __init__(self, **kwargs):
g = GridLayout(cols=3)
for item in self.food_database:
g.add_widget(CheckBox(id=item[0])) # checkbox first field
g.add_widget(Label(text=item[0]))
g.add_widget(CheckBox(id=item[0]+'_exclude')) # checkbox second field
# class FoodListView(ListItemCheckBox):
# pass
# class CustomFoodListView():
# food_database = [['salmon', 34, 23, 30, 300], ['another food', 34, 23, 2, 400], ['fat burger', 40, 34, 39, 500]]
#
# widgey = GridLayout(cols=3)
#
# for i in food_database:
# widgey.add_widget(CheckBox(id=i[0], size_hint=[0.2, 0.10]))
# widgey.add_widget(Label(text=i[0], size_hint=[0.2, 0.10]))
# widgey.add_widget(CheckBox(id=i[0]+'_exclude', size_hint=[0.2, 0.10]))
def sample_function(self):
print 'test fubar'
#
# search_data = search_page.text
# soup = BeautifulSoup(search_data, 'html5lib')
# print soup.prettify()
# self.dynamic_food_list.append('another fooder')
def add_food_item(self):
print 'test foo'
# print self.dynamic_food_list
# self.food_list.append("New Item here!")
# print self.dynamic_food_list
#
# # get the food item description/ details
# new_item = 'NEW ITEM!'
#
# # add to the listview
# self.food_list.adapter.data.extend([new_item])
#
# # reset the listview
# self.food_list._trigger_reset_populate()
def remove_food_item(self):
print 'foo2'
# print 'selection!:', self.food_list.adapter.selection
# for item in self.food_list.adapter.data:
# #print self.food_list.adapter.data
# print item
# #print item.text
# print item.active
#
# # if a list item is selected
# if self.food_list.adapter.selection:
#
# # get the text from the item selected
# selection = self.food_list.adapter.selection.text
#
# # remove the matching item
# self.food_list.adapter.data.remove(selection)
#
# # reset the listview
# self.food_list._trigger_reset_populate()
def edit_food_item(self):
print 'foo3'
# # if a list item is selected
#
# # get the text from the item selected
# selection = self.food_list.adapter.selection[0].text
#
# # remove the matching item
# self.food_list.adapter.data.remove(selection)
#
# # get the text from the item selected
# new_item = 'NEW ITEM!'
#
# # add the updated data to the list
# self.food_list.adapter.data.extend([new_item])
#
# # reset the listview
# self.food_list._trigger_reset_populate()
.kv file:
ScreenManagement:
#transition: FadeTransition()
MainScreen:
SettingsScreen:
MetricsScreen:
<MainScreen>:
name: "main"
GridLayout:
size_hint: 1, .9
pos_hint: {"top": 1}
cols: 2
#rows: 10
spacing: 8
BoxLayout:
orientation: "vertical"
#ListView:
#id: food_list_view
#pos_hint: {"left": 1, "top": 1}
#size_hint: 1, .9
#adapter:
#ListAdapter(data= root.food_database, cls=root.CustomFoodListView)
CustomFoodListView:
BoxLayout:
pos_hint: {"left": 1, "top": .9}
size_hint_y: None
height: "40dp"
size_hint: 1, .1
Button:
size_hint_x: 15
on_release: root.add_food_item()
text: "(+) Add Food Item"
Button:
size_hint_x: 15
on_release: root.remove_food_item()
text: "(-) Remove Food Item"
Button:
size_hint_x: 15
on_release: root.edit_food_item()
text: "Edit Food Item"
BoxLayout:
Button:
on_release: root.sample_function()
text: "Get Next Foods to Eat"
pos_hint: {"right": 1, "top": 1}
size_hint: 0.5, 0.1
BoxLayout:
size_hint: 1, .1
orientation: "horizontal"
pos_hint: {"bottom": 1}
spacing: 5
padding: 5
Button:
on_release:
app.root.current = "main"
root.manager.transition.direction = 'right'
text: "Home1"
font_size: 50
#background_normal: ''
#background_down: 'bkgnd-down-color.png'
#background_color: .88, .88, .88, 1
Button:
on_release:
app.root.current = "settings"
root.manager.transition.direction = 'left'
text: "Settings1"
font_size: 50
#background_normal: ''
#background_down: 'bkgnd-down-color.png'
#background_color: .88, .88, .88, 1
Button:
on_release:
app.root.current = "metrics"
root.manager.transition.direction = 'left'
text: "Progress1"
font_size: 50
#background_normal: ''
#background_down: 'bkgnd-down-color.png'
#background_color: .88, .88, .88, 1
<SettingsScreen>:
name: "settings"
If the checkboxes are able to be added to the MainScreen, I want to scan through the checkboxlist at any time to know what checkbox items are active.
Aucun commentaire:
Enregistrer un commentaire