Highlight a Kivy ToggleButton with mouse hovering

2020-04-11 18:41发布

问题:

I am currently coding a GUI with Kivy. I need to modify a ToggleButton behaviour so that it is highlighted when hovered by the mouse. Here is my code so far:

class FilterToggle(ToggleButton):

    def __init__(self, **kwargs):

        Window.bind(mouse_pos=self.on_mouse_pos)
        super(FilterToggle, self).__init__(**kwargs)

    def on_mouse_pos(self, *args):
        pos = args[1]
        if self.collide_point(*pos):
            print("I am on the good path!)

Here is my .kv file:

<FilterToggle>:

    text_size: self.width - 20, None
    valign: 'middle'
    halign: 'left'
    markup: True
    .
    .
    .
    FilterToggle:
        text: "This is just to illustrate"

The on_mouse_pos() function never prints anything, as self.collide_point(*pos) always returns "False". I found that self.pos gives me the coordinates of all my FilterToggle widgets, so there is obviously an issue with my code.

Thanks a lot for helping a Python beginner!