栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Kivy制作ToolTip?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Kivy制作ToolTip?

您可以改善和扩展的简单示例:

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()
方法。


编辑:根据此答案更新了代码



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号