Question: get current id of widget on python / kivy


get current id of widget on python / kivy

Answers 1
Added at 2016-12-15 08:12

i'm making a sort of android lock thing on kivy, and to draw the line, I need to get the id of the widget the mouse is on, so I assing an id to each one like this in the .kv file:

    id: one
    source: 'button.png'

and I know I can get all the ids (I have 9, of course), with the



but is there a way to get the ID the mouse is in? or the one I click? I have a hoverable class so it detects when it enters in a Widget, but I don't really know how to get its position, or change its source. Is there any:


or something like that? thanks for the help

Answers to

get current id of widget on python / kivy

nr: #1 dodano: 2016-12-15 14:12

You can use collide_widget or collide_point and in the widget set a method that will change a variable in the parent, let's say selected_widget to the current widget's like this:

if self.collide_point(*Window.mouse_pos):
    self.parent.selected_widget = self  # or its id

Then you can do with it anything. Maybe it'd be even better to put your logic into the widget itself and handle collision directly there. Obviously you'll need to bind a method you create with that if block above to an event such as on_release or on_press so run the method, otherwise it won't do a thing.

You can also get a hoverable behavior from this PR or even from this snippet.


Please note that the id will not be available in the widget instance

Which means == None and therefore to actually get id you need to do this:

def find(self, parent, widget):
    for id, obj in parent.ids.items():
        if obj == widget:
            print id
            return id
Source Show
◀ Wstecz