問題描述
當鼠標指針懸停在 ActionBar 中的圖標上時,我希望在 Qt 中看到工具提示.
是的,我可以使用 mode='spinner'
,但圖標更好.
I want to see tooltip as in Qt when the mouse pointer is hovering over icon in ActionBar.
Yes, I can use mode='spinner'
, but icons are nicer.
推薦答案
一個可以改進和擴展的簡單示例:
A simple example you can improve and extend:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.actionbar import ActionButton
from kivy.uix.label import Label
from kivy.clock import Clock
Builder.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):
pass
class 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):
pass
class 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()
方法.
First I bind on_mouse_pos
method to Window.mouse_pos
event so I can detect when the mouse cursor hovers over my subclass of ActionButton
. This is based on this snippet. Then I shedule an action with Clock.schedule_once()
to make my toolbox visible if I won't move my cursor. To display I'm just adding a subclass of Label to the stack of widgets. You can replace display_tooltip()
and close_tooltip()
methods with more sophisticated ones.
將代碼相應地更新為 this answer
這篇關于如何使用 Kivy 制作工具提示?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!