beginner here.
Im not completely sure if everything is correct in the code bellow, but it seems to work for making the size of the buttons created by recycleview, to adapt to each button's text.
Question: How can i access (print for now) the clicked button's id in the on_release_m method?
Thanks for your time!
from kivy.config import Config
Config.set('graphics', 'width', '270')
Config.set('graphics', 'height', '550')
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.lang import Builder
# I included this in case you have to use it for the answer instead of the python code:
Builder.load_string("""
<MyRecycleBoxLayout>:
# orientation: 'vertical'
# default_size: None, None
# default_size_hint: 1, None
# size_hint_y: None
# height: self.minimum_height
<MyButton>:
# font_size: 30
# text_size: self.width, None
# size_hint_y: None
# height: self.texture_size[1]
""")
class MyRecycleBoxLayout(RecycleBoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.orientation= 'vertical'
self.default_size= (None, None)
self.default_size_hint=( 1, None)
self.size_hint_y= None
self.bind(minimum_height=self.setter("height"))
class MyButton(Button):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.font_size=30
self.bind(width=lambda s, w: s.setter("text_size")(s, (w, None)))
self.size_hint_y = None
self.bind(texture_size=self.setter("size"))
class RV(RecycleView):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.myRecycleBoxLayout=MyRecycleBoxLayout()
self.data.clear()
for i in range(150):
self.data.append({f"text":f"{i} "*50,"id": f"btn_{i}","on_release":self.on_release_m})
self.add_widget(self.myRecycleBoxLayout)
def on_release_m (self,*args):
#Here print the clicked button id and not the first's:
print(self.data[0]["id"])
pass
class RecApp(App):
def build(self):
self.rv=RV()
self.rv.viewclass=MyButton
return self.rv
RecApp().run()
You can pass additional information to your on_release_m()
method by using partial
:
for i in range(150):
self.data.append({f"text": f"{i} "*50, "id": f"btn_{i}", "on_release": partial(self.on_release_m, i)})
Then, in the on_release_m()
method:
def on_release_m(self, i):
#Here print the clicked button id and not the first's:
print(self.data[i]["id"])