Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

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

51 views
Skip to first unread message

Degenerate Tech

unread,
Apr 19, 2024, 9:12:36 AM4/19/24
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 AM4/19/24
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 PM4/19/24
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.

Henrik R.

unread,
Aug 27, 2024, 9:11:39 AM8/27/24
to Kivy users support
Thank you for this example on how to use:

    index_x = parent_widget.children.index(a_child_widget)

I cannot see anything about this index method in the documentation. I looked in https://kivy.org/doc/stable/api-kivy.uix.widget.html and https://kivy.org/doc/stable/guide/widgets.html .

Here is how it looks in my app:

def replace_button_with_label(self):
    index_b = self.children.index(self.choose_first_target_button)
    self.remove_widget(self.children[index_b])
    self.target_text_label = ScalableLabel()
    self.set_target_label_text()
    self.add_widget(self.target_text_label, index=index_b)

And here is a minimal example on how to replace a Button with a Label:

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button

box = BoxLayout(orientation='vertical')
lab1 = Label(text="Text-1")
but = Button(text="Touch me") # NOT butt...
lab3 = Label(text="Text-3")

box.add_widget(lab1)
box.add_widget(but)
box.add_widget(lab3)

index_b = box.children.index(but)
print(index_b)

lab2 = Label(text="Text-2")
box.remove_widget(box.children[index_b])
box.add_widget(lab2, index=index_b)

ElliotG

unread,
Aug 27, 2024, 10:23:53 AM8/27/24
to Kivy users support
index() is a method of a python list.  See the documentation for python:https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range
Look in the table under common sequence operations.


Henrik R.

unread,
Aug 27, 2024, 11:19:52 AM8/27/24
to Kivy users support
Aaah! I wasn't aware that you can treat the list of child widgets as a Python list. Great. :-)
Reply all
Reply to author
Forward
0 new messages