gpt4 book ai didi

python - 悬停在Kivy ActionBar ActionButton上

转载 作者:行者123 更新时间:2023-12-03 08:19:31 24 4
gpt4 key购买 nike

根据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()

关于python - 悬停在Kivy ActionBar ActionButton上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60985374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com