Kivy: Bring widget to front(Kivy:将小部件放在前面)
问题描述
ActionBar 中的图像重叠Toolbar.(工具栏是
你应该调用 add_widget() 和 remove_widget() 而不是从 self (这是您的 ActionButton),但来自层次结构中较高的对象.您可以存储对 ActionBar 父级的引用或仅使用 Window 对象本身:
from kivy.core.window 导入窗口# ...类 MyActionButton(ActionButton):# ...def close_tooltip(self, *args):Window.remove_widget(self.tooltip)def display_tooltip(self, *args):Window.add_widget(self.tooltip)请注意,这可能会更改工具提示小部件的计算大小.
我更新了参考答案.
Images in ActionBar overlap Toolbar. (Toolbar is Bubble with Label)
My code is based on this answer.
Example for ActionBar button:
TooltipButton:
icon: 'images/32/quit.png'
text: _('Quit')
on_press: quit()
TooltipButton 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)
Tooltip rule (superclass is Bubble):
<Tooltip>:
tip: tip
Label:
id: tip
text_size: self.size
halign: 'center'
text: 'Tip'
You should call add_widget() and remove_widget() not from self (which is your ActionButton) but from an object that is higher in the hierarchy. You can store a reference to a parent of ActionBar or just use Window object itself:
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)
Note that this will probably change the computed size of your tooltip widget.
I updated referenced answer.
这篇关于Kivy:将小部件放在前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Kivy:将小部件放在前面
基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
