Highlight a Kivy ToggleButton with mouse hovering

大城市里の小女人 提交于 2019-12-02 02:24:00

问题


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!

来源:https://stackoverflow.com/questions/38337947/highlight-a-kivy-togglebutton-with-mouse-hovering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!