您可以改善和扩展的简单示例:
from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.widget import Widgetfrom kivy.core.window import Windowfrom kivy.uix.actionbar import ActionButtonfrom kivy.uix.label import Labelfrom kivy.clock import ClockBuilder.load_string("""<Tooltip>: size_hint: None, None size: self.texture_size[0]+5, self.texture_size[1]+5 canvas.before: Color: rgb: 0.2, 0.2, 0.2 Rectangle: size: self.size pos: self.pos<MyWidget> ActionBar: ActionView: MyActionButton: icon: 'atlas://data/images/defaulttheme/audio-volume-high' MyActionButton: icon: 'atlas://data/images/defaulttheme/audio-volume-high' """)class Tooltip(Label): passclass MyActionButton(ActionButton): tooltip = Tooltip(text='Hello world') def __init__(self, **kwargs): Window.bind(mouse_pos=self.on_mouse_pos) super(ActionButton, self).__init__(**kwargs) def on_mouse_pos(self, *args): if not self.get_root_window(): return pos = args[1] self.tooltip.pos = pos Clock.unschedule(self.display_tooltip) # cancel scheduled event since I moved the cursor self.close_tooltip() # close if it's opened if self.collide_point(*self.to_widget(*pos)): Clock.schedule_once(self.display_tooltip, 1) def close_tooltip(self, *args): Window.remove_widget(self.tooltip) def display_tooltip(self, *args): Window.add_widget(self.tooltip)class MyWidget(Widget): passclass ClientApp(App): def build(self): return MyWidget()if __name__ == '__main__': ClientApp().run()首先,我将
on_mouse_pos方法绑定到
Window.mouse_pos事件,以便可以检测到鼠标光标悬停在的子类上
ActionButton。这是基于此片段的。然后,
Clock.schedule_once()如果不移动光标,我将采取行动使工具箱可见。为了显示,我只是将Label的子类添加到小部件堆栈中。您可以使用更复杂的方法替换
display_tooltip()和
close_tooltip()方法。
编辑:根据此答案更新了代码



