Kivy:将小部件放在前面
Kivy: Bring widget to front
ActionBar 中的图像与 Toolbar 重叠。 (工具栏 是 Bubble 和 标签)
我的代码基于 答案。
ActionBar 按钮示例:
TooltipButton:
icon: 'images/32/quit.png'
text: _('Quit')
on_press: quit()
工具提示按钮 class:
class TooltipButton(ActionButton):
tooltip = Tooltip()
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):
self.remove_widget(self.tooltip)
def display_tooltip(self, *args):
self.tooltip.tip.text = self.text
self.add_widget(self.tooltip)
工具提示规则(superclass是气泡):
<Tooltip>:
tip: tip
Label:
id: tip
text_size: self.size
halign: 'center'
text: 'Tip'
您应该调用 add_widget()
和 remove_widget()
,而不是从 self
(这是您的 ActionButton
),而是从层次结构中更高的对象。您可以存储对 ActionBar
父对象的引用,或者只使用 Window
对象本身:
from kivy.core.window import Window
# ...
class MyActionButton(ActionButton):
# ...
def close_tooltip(self, *args):
Window.remove_widget(self.tooltip)
def display_tooltip(self, *args):
Window.add_widget(self.tooltip)
请注意,这可能会更改工具提示小部件的计算大小。
我更新了参考答案。
ActionBar 中的图像与 Toolbar 重叠。 (工具栏 是 Bubble 和 标签)
我的代码基于
ActionBar 按钮示例:
TooltipButton:
icon: 'images/32/quit.png'
text: _('Quit')
on_press: quit()
工具提示按钮 class:
class TooltipButton(ActionButton):
tooltip = Tooltip()
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):
self.remove_widget(self.tooltip)
def display_tooltip(self, *args):
self.tooltip.tip.text = self.text
self.add_widget(self.tooltip)
工具提示规则(superclass是气泡):
<Tooltip>:
tip: tip
Label:
id: tip
text_size: self.size
halign: 'center'
text: 'Tip'
您应该调用 add_widget()
和 remove_widget()
,而不是从 self
(这是您的 ActionButton
),而是从层次结构中更高的对象。您可以存储对 ActionBar
父对象的引用,或者只使用 Window
对象本身:
from kivy.core.window import Window
# ...
class MyActionButton(ActionButton):
# ...
def close_tooltip(self, *args):
Window.remove_widget(self.tooltip)
def display_tooltip(self, *args):
Window.add_widget(self.tooltip)
请注意,这可能会更改工具提示小部件的计算大小。
我更新了参考答案。