how to get index of a widget which in present in boxlayout

20 views
Skip to first unread message

Degenerate Tech

unread,
Apr 19, 2024, 9:12:36 AMApr 19
to Kivy users support
i have two widget wid1 and wid2  where wid1 already present in a boxlayout i want to replace wid1 by wid2 in same index so . widget position be same .
how to do ? for that i need index of that widget . how to read ? that

Degenerate Tech

unread,
Apr 19, 2024, 9:24:12 AMApr 19
to Kivy users support
solved by using python list indexing .
["foo", "bar", "baz"].index("bar")

elli...@cox.net

unread,
Apr 19, 2024, 4:37:54 PMApr 19
to Kivy users support
The children list of the boxlayout is a list.  You can use the list index() method and pass in the widget and it will return the appropriate index.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button

kv =
"""
BoxLayout:
   ScrollView:
       BoxLayout:
           id: box
           orientation: 'vertical'
           size_hint_y: None
           height: self.minimum_height
"""

class IndexButton(Button):
   
def on_release(self):
       app = App.get_running_app()
       children = app.root.ids.box.children
       index = children.index(
self)
       
print(f'This button is at index: {index}')

class IndexWidgetDemo(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       box =
self.root.ids.box
       
for i in range(20):
           box.add_widget(IndexButton(
text=str(i), size_hint_y=None, height=48))

IndexWidgetDemo().run()
You can also see the children list is the opposite order of insertion. 



From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Degenerate Tech <sksah...@gmail.com>
Sent: Friday, April 19, 2024 6:12 AM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] how to get index of a widget which in present in boxlayout
 
i have two widget wid1 and wid2  where wid1 already present in a boxlayout i want to replace wid1 by wid2 in same index so . widget position be same .
how to do ? for that i need index of that widget . how to read ? that

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e37eee5e-17c9-4e9d-99f2-4e03fffe95e6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages