Kivy:将小部件置于最前面 [英] Kivy: Bring widget to front

查看:71
本文介绍了Kivy:将小部件置于最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ActionBar 中的图像与工具栏重叠. (工具栏 气泡 标签)
我的代码基于答案.

Images in ActionBar overlap Toolbar. (Toolbar is Bubble with Label)
My code is based on this answer.

ActionBar按钮示例:

TooltipButton:
    icon: 'images/32/quit.png'
    text: _('Quit')
    on_press: quit()

TooltipButton类:

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)

工具提示规则(超类为Bubble):

Tooltip rule (superclass is Bubble):

<Tooltip>:
    tip: tip
    Label:
        id: tip
        text_size: self.size
        halign: 'center'
        text: 'Tip'

推荐答案

您不应从self(即您的ActionButton)而是从层次结构中较高的对象调用add_widget()remove_widget() .您可以存储对ActionBar父级的引用,也可以只使用Window对象本身:

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.

我更新了参考答案.

这篇关于Kivy:将小部件置于最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆