根据This Github File,我试图在python 3.7.5 Windows 10平台中为操作按钮创建一个悬停。这是我尝试的:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.actionbar import *
from kivy.properties import ObjectProperty
from kivy.properties import BooleanProperty
class HoverBehavior(object):

    hovered = BooleanProperty(False)
    border_point= ObjectProperty(None)
    def __init__(self, **kwargs):
        self.register_event_type('on_enter')
        self.register_event_type('on_leave')
        Window.bind(mouse_pos=self.on_mouse_pos)
        super(HoverBehavior, self).__init__(**kwargs)

    def on_mouse_pos(self, *args):
        if not self.get_root_window():
            return # do proceed if I'm not displayed <=> If have no parent
        pos = args[1]
        #Next line to_widget allow to compensate for relative layout
        inside = self.collide_point(*self.to_widget(*pos))
        if self.hovered == inside:
            #We have already done what was needed
            return
        self.border_point = pos
        self.hovered = inside
        if inside:
            self.dispatch('on_enter')
        else:
            self.dispatch('on_leave')

    def on_enter(self):
        pass

    def on_leave(self):
        pass
from kivy.factory import Factory
Factory.register('HoverBehavior', HoverBehavior)
Builder.load_string("""
<TitleBar>:
    ActionBar:
        pos_hint: {'top':1}
        width: 50
        color: [ 1, 1, 1, 1]
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Hello'
                with_previous: False
                color: [ 0, 0, 0, 1]
            ActionOverflow:
            ActionButton:
                icon: 'icons/app_close_init.png' if self.hovered else "icons/app_close_hover.png"
                on_press: app.closing()
            ActionButton:
                important: True
                text: 'Important'
                color: [ 0, 0, 0, 1]
            ActionButton:
                text: 'Btn2'
            ActionButton:
                text: 'Btn3'
            ActionButton:
                text: 'Btn4'
""")
class TitleBar(FloatLayout):
    pass

class TetraApp(App):

    def build(self):
        Window.size=(875,575)
        Window.clearcolor = (1, 1, 1, 1)
        Window.borderless=True
        return TitleBar()
    def on_press_button(self):
        return self.root.add_widget(Label(text='Hello'))
    def closing(self):
        app.stop()
if __name__=='__main__':
    app=TetraApp()
    app.run()

错误提示“操作按钮”没有属性hovered,但我已定义HoverBehaviour类。我做错了什么?当鼠标悬停在其上时,操作按钮用于将图标图像从一个图像更改为另一个图像。
编辑过
还尝试了:
class HoverBehavior(object):
    def __init__(self, **kwargs):
        self.hovered = BooleanProperty(False)
        self.border_point= ObjectProperty(None)
        self.register_event_type('on_enter')
        self.register_event_type('on_leave')
        Window.bind(mouse_pos=self.on_mouse_pos)
        super(HoverBehavior, self).__init__(**kwargs)

    def on_mouse_pos(self, *args):
        if not self.get_root_window():

但这也显示了Attribute Error

最佳答案

您已经定义了HoverBehavior,但实际上并未使用它,因此ActionButtonHoverBehavior一无所知。要使用它,请定义自己的自定义ActionButton,例如:

class MyActionButton(HoverBehavior, ActionButton):
    pass

然后,在kv文件中,将ActionButton替换为MyActionButton:
        MyActionButton:
            icon: 'icons/app_close_init.png' if self.hovered else "icons/app_close_hover.png"
            on_press: app.closing()

09-18 02:47