悬停在Kivy ActionBar ActionButton上 [英] On Hover for Kivy ActionBar ActionButton
问题描述
根据此Github文件,我试图创建将鼠标悬停在python 3.7.5 Windows 10平台中的操作按钮上.这就是我尝试过的:
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()
错误表明操作按钮没有属性hovered
,但是我定义了HoverBehaviour
类.我做错了什么?鼠标悬停时,操作按钮用于将图标图像从一个图像更改为另一个图像.
已编辑
还尝试过:
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():
但这也显示了Attribute Error
.
推荐答案
您已经定义了HoverBehavior
,但实际上并没有使用它,因此ActionButton
对HoverBehavior
一无所知.要使用它,请定义自己的自定义ActionButton
,例如:
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
然后,在您的kv
文件中,将ActionButton
替换为MyActionButton
:
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()
这篇关于悬停在Kivy ActionBar ActionButton上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!