Kivy:将小部件放在前面

2022-01-15 00:00:00 python kivy user-interface

问题描述

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.

相关文章