I saw different posts talking about that: HERE and THERE.
But they are complex solutions when I just want to understand what is the exact line that make the changement. I have the access to the RecycleView
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory as F
from kivy.properties import ListProperty
KV = '''
<Item>:
RecycleView:
data: app.data
viewclass: 'Row'
RecycleBoxLayout:
id: recycle_view
orientation: 'vertical'
size_hint: 1, None
height: self.minimum_height
default_size_hint: 1, None
default_size: 0, dp(40)
'''
def _on_text(instance, value):
recycle_view = instance.app.screen.ids["recycle_view"]
print(recycle_view,"value that I want to update in the recycle_view:",value)
#recycle_view.data ? I don't know how to access the data and change it !
class Row(F.TextInput):
def on_kv_post(self, base_widget):
super().on_kv_post(base_widget)
self.app = MDApp.get_running_app()
self.bind(text=_on_text)
class Application(MDApp):
data = ListProperty()
def build(self):
self.data = [
{'index': i, 'text': 'hello {}'.format(i)}
for i in range(40)
]
self.screen = Builder.load_string(KV)
return self.screen
if __name__ == "__main__":
Application().run()
Okay, I find out that I missed the fact that I was playing with RecycleBoxlaout and not RecycleView.
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory as F
from kivy.properties import ListProperty
KV = '''
<Item>:
RecycleView:
data: app.data
viewclass: 'Row'
RecycleBoxLayout:
id: recycle_boxlayout
orientation: 'vertical'
size_hint: 1, None
height: self.minimum_height
default_size_hint: 1, None
default_size: 0, dp(40)
'''
def _on_text(instance, value):
instance.app.screen.data[instance.index]["text"] = value
class Row(F.TextInput):
def on_kv_post(self, base_widget):
super().on_kv_post(base_widget)
self.app = MDApp.get_running_app()
self.counter = -1
self.bind(text=_on_text)
class Application(MDApp):
data = ListProperty()
def build(self):
self.data = [
{'index': i, 'text': 'hello {}'.format(i)}
for i in range(40)
]
self.screen = Builder.load_string(KV)
return self.screen
if __name__ == "__main__":
Application().run()
Related
I made a recycleview list of custom cards. For each card i want to pass a custom object called "show", its type is "Content".
The class of custom card is "StreamingShowCard". I can't create the card by KVlang because its class contains some pytho methods.
This is the code:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.behaviors import RoundedRectangularElevationBehavior
from kivymd.uix.card import MDCard
from kivy.properties import StringProperty
from kivy.uix.behaviors import ButtonBehavior
from kivymd.uix.list import OneLineIconListItem
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.gridlayout import MDGridLayout
from kivymd.uix.fitimage import FitImage
from kivy.uix.image import AsyncImage
from kivymd.uix.button import MDFlatButton
#import datetime
from datetime import datetime
from kivy.uix.image import Image
from kivymd.uix.dialog import MDDialog
from kivymd.uix.screen import MDScreen
from kivymd.uix.responsivelayout import MDResponsiveLayout
from kivymd.uix.progressbar import MDProgressBar
from kivy.clock import Clock
from kivymd.uix.button import MDIconButton
from kivymd.uix.list import IconLeftWidget
from kivymd.uix.recycleview import MDRecycleView
from kivy import properties
KV = '''
<Cover>:
<StreamingShowCard>:
show: root.show
MDScreen:
MDRecycleView:
size_hint_y: 1
size_hint_x: 1
viewclass: 'StreamingShowCard'
id: rv
MDRecycleGridLayout:
cols: 2
height: self.minimum_height
size_hint_y: None
row_default_height: '250dp'
row_force_default: True
padding: dp(10)
spacing: dp(10)
MDFloatingActionButton:
id: fab
icon: "plus"
pos_hint: {"center_x": .5, "center_y": .1}
on_release: app.add_item()
'''
class MD3Card(MDCard, RoundedRectangularElevationBehavior):
'''Implements a material design v3 card.'''
text = StringProperty()
class Content: #eg: film found
name = ""
url = ""
platform = ""
imageUrl = ""
Image = False
#free = False
def __init__(self, name, url, platform, imageUrl):
self.name = name
self.url = url
self.platform = platform
self.imageUrl = imageUrl
class StreamingShowCard(MD3Card):
#show = None #the object of Content class attached to the
show = properties.ObjectProperty()
def __init__(self, **kwargs):
super(StreamingShowCard, self).__init__(**kwargs)
self.__set_cardGraphic()
self.image = Cover(size_hint_min_y=0.7, source="https://kivy.org/doc/stable/_static/logo-kivy.png", opacity= 100 if True else 0, radius= ["10dp", "10dp", "0dp", "0dp"])
grid1 = MDGridLayout(cols=1, size_hint_x=1, size_hint_y=1, rows=3)
grid1.add_widget(self.image)
grid1.add_widget(MDLabel(text=self.show, size_hint_y = 0.3, font_style='Subtitle2', halign='left', valign='middle'))
box1 = MDBoxLayout(orientation = "horizontal", size_hint_x=1, size_hint_y=0.2)
box1.add_widget(MDLabel(text="self.show.platform", size_hint_y = 1, size_hint_x=0.8, theme_text_color="Secondary", font_style='Caption', halign='left', valign='middle'))
grid1.add_widget(box1)
self.add_widget(grid1)
def __set_cardGraphic(self):
self.md_bg_color = "#18222c"
#self.size_hint_y = None
self.elevation = 10
self.padding = "1dp"
self.size_hint_x = 1
self.radius = "10dp"
self.ripple_behavior = True
self.on_press = self.cardPress
def cardPress(self, *args):
dialog = MDDialog(
title = "Aprire la pagina in un browser?",
text = "Text",#self.show.name,
size_hint = (0.8, 0.8),
auto_dismiss = False,
buttons=[
MDFlatButton(text="No", text_color=self.theme_cls.primary_color, on_release=lambda x: dialog.dismiss()),
MDFlatButton(text="Si", text_color=self.theme_cls.primary_color, on_release=lambda x: yesClick())
]
)
dialog.open()
def yesClick():
#openUrl(self.show.url)
dialog.dismiss(force=True)
def loadImage(self, *args):
if not self.show.Image:
self.show.loadImage(True)
self.image.source = self.show.imageUrl
self.image.opacity = 100
self.image.reload()
class Cover(AsyncImage, FitImage):
pass
class MainApp(MDApp):
def build(self):
return Builder.load_string(KV)
def add_item(self):
for i in range(100):
self.root.ids.rv.data.append({
"show": Content("name", "www.google.com", "platform", "https://kivy.org/doc/stable/_static/logo-kivy.png")
})
MainApp().run()
If the recycleview's viewclass is a Label i can pass the text directly from main by appending dict to RV data, but whit this custom class of recycle view i cannot pass Content to show property.
Is it possible to populate the RecycleView by the main?
Couldn't get your code to run, but if you want to use an ObjectProperty in the data of a RecycleView, you need to handle it a bit differently. Here is an example of using an ObjectProperty in the data:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.recycleview import RecycleView
Builder.load_string('''
<MyObject>:
size_hint_y: None
height: 100
Label:
id: label
text: root.text # uses the text StringProperty
size_hint: None, None
size: 200, 100
<RV>:
viewclass: 'MyObject'
RecycleBoxLayout:
default_size: None, 100
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
''')
class MyObject(BoxLayout):
show = ObjectProperty(None)
text = StringProperty('Abba')
def on_show(self, instance, new_obj):
# handle the ObjectProperty named show
if new_obj.parent:
# remove this obj from any other MyObject instance
new_obj.parent.remove_widget(new_obj)
for ch in self.children:
if isinstance(ch, Image):
# remove any previous obj instances
self.remove_widget(ch)
break
# add the new obj to this MyObject instance
self.add_widget(new_obj)
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'text': str(x),
'show': Image(source='tester.png', size_hint=(None, None), size=(100, 100),
allow_stretch=True, keep_ratio=True)}
for x in range(100)]
class TestApp(App):
def build(self):
return RV()
if __name__ == '__main__':
TestApp().run()
For some reason MDExpansionPanel doesn't behave as it should in a RecycleView, each time you expand the list, the more it deviates from the pattern. I don't know if there is something wrong with my code or the MDExpansionPanel was not made for a RecycleView
main.py
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import (MDExpansionPanel,
MDExpansionPanelOneLine)
Builder.load_string("""
<MyRecycleView>:
viewclass: "MyExpansionPanel"
effect_cls: "ScrollEffect"
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
spacing: dp(10)
<Content>:
adaptive_height: True
IconLeftWidget:
icon: "account"
""")
class MyRecycleView(RecycleView):
pass
class Content(MDBoxLayout):
pass
class MyExpansionPanel(MDExpansionPanel, RecycleDataViewBehavior):
text = StringProperty()
def __init__(self, **kwargs):
self.panel_cls = MDExpansionPanelOneLine()
super().__init__(**kwargs)
def refresh_view_attrs(self, rv, index, data):
self.panel_cls.text = data["text"]
return super().refresh_view_attrs(rv, index, data)
class MyApp(MDApp):
def build(self):
recycle = MyRecycleView()
recycle.data = [
{
"icon":"arrow-right",
"text":"Test",
"content":Content()
}for _ in range(5)
]
return recycle
MyApp().run()
Result:
Any idea how to resolve this?
What i'm trying to do is find a way to refresh a table made by using recycleview,
If you press one of the label created, my code delete data from table, but not refresh the table on a graphic level
Obviously if you press the same label 2 times, python gives ValueError...
Here's my code:
temp.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.properties import BooleanProperty
from kivy.properties import ListProperty
class Sfondo_tabella(RecycleDataViewBehavior, Label):
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos) and self.selectable:
x = Screen_1()
x.remove(self.text)
class Screen_1(Screen):
data_items = ListProperty([])
data_items = ['1', '2', '3']
def remove(self, data):
self.data_items.remove(data)
class TempApp(App):
pass
if __name__ == '__main__':
TempApp().run()
temp.kv
ScreenManager:
id: screen_manager
name: "screen_manager"
Screen_1:
id: screen_1
name: "Screen_1"
manager: screen_manager
<Sfondo_tabella>:
# Draw a background to indicate selection
color: 0,0,0,1
font_size: self.height * 0.5
text_size: self.width, None
valign: 'top'
halign: 'center'
canvas.before:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba:0,0,0,1
Line:
width:0.5
rectangle:(self.x,self.y,self.width,self.height)
<Screen_1>:
BoxLayout:
RecycleView:
viewclass: 'Sfondo_tabella'
data: [{'text': str(x)} for x in root.data_items]
RecycleGridLayout:
cols: 10
size_hint: 1, None
default_size: None, dp(20)
default_size_hint: 1, None
height: self.minimum_height
width: self.minimum_width
In your code you are not updating the RecycleView data. The refresh_view_attrs function should be overloaded to get the index of the label clicked/touched upon and then the data needs to be updated accordingly. Following is the updated code for temp.py:
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.properties import BooleanProperty
from kivy.properties import ListProperty
class Sfondo_tabella(RecycleDataViewBehavior, Label):
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
self.index = index
return super(Sfondo_tabella, self).refresh_view_attrs(rv, index, data)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos) and self.selectable:
x = Screen_1()
x.remove(self.text)
self.parent.parent.data.pop(self.index)
class Screen_1(Screen):
data_items = ListProperty([])
data_items = ['1', '2', '3']
def remove(self, data):
self.data_items.remove(data)
class TempApp(App):
pass
if __name__ == '__main__':
TempApp().run()
This will update properly the data of RecycleView and the GUI accordingly.
I'm trying to better understand how the RecycleView functions. Seems like only practical examples will teach me. Docs aren't helping. Kindly have a look at my current screen in the pic below.
Now following are what I'm trying to achieve.
Add/Remove new rows to/from the list.
The first column sl/no should maintain the order even after deleting from in between.
How to achieve 'sort' function? Is it by taking the data into python and do the sort and then update that to the RV again ?
Please find the both .py and .kv codes below.
rv_main.py
import os
os.environ['KIVY_GL_BACKEND'] = 'gl'
import kivy
kivy.require('1.11.0')
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.properties import ListProperty, BooleanProperty
from kivy.properties import NumericProperty
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
#-----------------------------------------------------------------------
class RecycleViewRow(RecycleDataViewBehavior, BoxLayout):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
slno = StringProperty('')
typ = StringProperty('')
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(RecycleViewRow, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(RecycleViewRow, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
pass
else:
pass
def delete_row(self,rv, index, is_selected):
if is_selected:
rv.data.pop(index)
rv.layout_manager.clear_selection()
#-----------------------------------------------------------------------
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
#fetch data from the database
self.data = [{'slno': str(x+1),'typ': 'default'} for x in range(4)]
#-----------------------------------------------------------------------
class DataTable(BoxLayout):
def addRow(self):
pass
def removeRow(self):
pass
#-----------------------------------------------------------------------
class RvMainApp(App):
def build(self):
return DataTable()
if __name__ == '__main__':
RvMainApp().run()
rvmain.kv
#: kivy 1.11.0
<RecycleViewRow>:
id: rv
slno: ""
typ: ""
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0.4, 0.4, 0.4, 1)
Rectangle:
pos: self.pos
size: self.size
orientation: 'horizontal'
size_hint: 1.0, 1.0
Label:
text: root.slno
size_hint_x : 1.0
Label:
text: root.typ
size_hint_x : 1.0
#----------------------------------------------------------------
<RV>:
id : rv
viewclass: 'RecycleViewRow'
SelectableRecycleBoxLayout:
default_size: None, dp(40)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
#----------------------------------------------------------------
<DataTable>:
orientation : 'vertical'
Button:
BoxLayout:
Button:
RV:
Button:
BoxLayout:
Button:
text: "Add"
on_release: rv.data.append({"slno": "?", "typ": 'default'})
Button:
text: "Remove"
on_release:
You edit the data list of the recycleview. The data will be sorted as it is sorted in that list.
So here is an example of the add remove feature:
from kivy.app import App
from kivy.lang import Builder
KV = '''
<Row#BoxLayout>:
ind: 1
Button:
text: str(root.ind)
Button:
text: "default"
BoxLayout:
ind: 1
orientation: "vertical"
Button:
BoxLayout:
Button:
RecycleView:
id: rv
data: [{"text":"first","ind":1}]
viewclass: 'Row'
RecycleBoxLayout:
default_size_hint: 1, None
default_size: None, dp(56)
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Button
BoxLayout:
Button:
text: "Add"
on_release:
root.ind += 1
rv.data.append({"ind": root.ind})
Button:
text: "Remove"
on_release:
root.ind = root.ind - 1 if root.ind > 0 else 0
if len(rv.data): rv.data.pop(-1)
'''
class Test(App):
def build(self):
self.root = Builder.load_string(KV)
return self.root
Test().run()
And here is an example on how to sort the data list by some key. In this case ind.
from kivy.app import App
from kivy.lang import Builder
KV = '''
RecycleView:
viewclass: 'Label'
data: sorted([{"text":"!", "ind":3},{"text":"world", "ind":2},{"text":"hello", "ind":1}], key=lambda k: k["ind"])
RecycleBoxLayout:
id: layout
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
'''
class TestApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == '__main__':
TestApp().run()
How can I pass data from RequestRecycleView to refresh_view_data? I tried with global variables and instantiating data in RequestRecycleView but still can't trigger refresh_view_data by appending Observable data. It is working when I return RequestRecycleView as root but I want ScreenManager to be my root.
from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from random import sample
from string import ascii_lowercase
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager:
transition: FadeTransition()
RequestsScreen:
<RequestRow#BoxLayout>
size_hint_y: None
orientation: 'vertical'
row_index: id_row_index.text
row_index:''
pos: self.pos
size: self.size
Label:
id: id_row_index
text: root.row_index
<RequestRecycleView#RecycleView>:
#id: rv
viewclass: 'RequestRow'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: True
touch_multiselect: True
<RequestsScreen>
name: 'RequestsScreen'
BoxLayout:
orientation: 'vertical'
Label:
text: 'recycle'
RequestRecycleView:
"""
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class RequestRow(RecycleDataViewBehavior):
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
self.row_index = str(index)
self.row_content = data['text']
return super(RequestRow, self).refresh_view_attrs(
rv, index, data)
class ScreenManagement(ScreenManager):
pass
class RequestRecycleView(RecycleView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.data = []
for r in range(30):
row = {'text': ''.join(sample(ascii_lowercase, 6))}
self.data.append(row)
class RequestsScreen(Screen):
pass
Builder.load_string(kv)
sm = ScreenManagement()
sm.add_widget(RequestsScreen(name = 'requests'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
By modifying
<RequestRow#BoxLayout>
size_hint_y: None
orientation: 'vertical'
row_index: id_row_index.text
row_index:''
pos: self.pos
size: self.size
Label:
id: id_row_index
text: root.row_index
to
<RequestRow#BoxLayout>
text: 'abba'
size_hint_y: None
orientation: 'vertical'
row_index: id_row_index.text
row_index:''
pos: self.pos
size: self.size
Label:
id: id_row_index
text: self.parent.text
results in working code. Note that the dictionary keys in the data are expected to be attributes of the viewclass. The added text attribute in RequestRow, provides that attribute, and the text: self.parent.text in the Label places that text (from the viewclass) into the Label. Also, you can replace the lines:
Builder.load_string(kv)
sm = ScreenManagement()
sm.add_widget(RequestsScreen(name = 'requests'))
with just:
sm = Builder.load_string(kv)
since your kv file specifies the ScreenManager as the root object.