问题
According to This Github File, I was trying to create a hover for an action button in python 3.7.5 windows 10 platform. This is what i tried:
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()
The Error says that Action Button has no attribute hovered
but I have defined the HoverBehaviour
class. What have I done Wrong? The action button is meant to change the icon image from one to another when the mouse hovers on it.
EDITED
Also tried:
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():
But that too shows the Attribute Error
.
回答1:
You have defined your HoverBehavior
, but you haven't actually used it, so the ActionButton
knows nothing about HoverBehavior
. To use it, define your own custom ActionButton
like:
class MyActionButton(HoverBehavior, ActionButton):
pass
Then, in your kv
file, replace ActionButton
with MyActionButton
:
MyActionButton:
icon: 'icons/app_close_init.png' if self.hovered else "icons/app_close_hover.png"
on_press: app.closing()
来源:https://stackoverflow.com/questions/60985374/on-hover-for-kivy-actionbar-actionbutton