Kivy:越过应用程序窗口的左边缘或上边缘时,自定义鼠标光标的不良行为

时间:2018-12-19 12:46:28

标签: python kivy

我想用猕猴桃做一个自定义的鼠标光标。 这是我目前所拥有的:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.scatter import Scatter
from kivy.core.window import Window
#Window.show_cursor = False

KV = """
FloatLayout
    BoxLayout
        MyTextInput
    MyMouse


<MyTextInput>:
    font_size: 40
    text: 'Some text'

<MyMouse>:
    mouse_im_size: mouse_im.size
    auto_bring_to_front: True

    do_rotation:False
    do_scale:False
    do_translation_y:False

    Image
        id: mouse_im
        size: 100, 100 / self.image_ratio
        source: 'cursor-pink.png'

"""

class MyTextInput(TextInput):
    pass

class MyMouse(Scatter):
    def __init__(self, **kwargs):
        Window.bind(mouse_pos=self.on_mouse_pos)
        super(MyMouse, self).__init__(**kwargs)

    def on_touch_down(self, *touch):
        return

    def on_mouse_pos(self, *args):
        x,y = args[1]
        self.pos = [x,y-self.mouse_im_size[1]]


class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)

MyApp().run()

问题是,当我将鼠标移到应用程序的左边缘或上边缘之外时,光标图像仍保留在应用程序内,并且我希望鼠标图像消失,就像我将鼠标移到右边缘或下边缘之外时一样

问题似乎是on_mouse_pos()仅在鼠标位于窗口内时有效。

found a way获取鼠标在窗口外部时的位置,但是我不知道如何将其用于任务。也许有更好的方法可以做到这一点。

2 个答案:

答案 0 :(得分:1)

您可以通过使用Window事件on_cursor_enteron_cursor_leave并通过使用opacity属性使光标可见/不可见来实现此目的:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.scatter import Scatter
from kivy.core.window import Window
#Window.show_cursor = False

KV = """
FloatLayout
    BoxLayout
        MyTextInput
    MyMouse
        id: themouse


<MyTextInput>:
    font_size: 40
    text: 'Some text'

<MyMouse>:
    mouse_im_size: mouse_im.size
    auto_bring_to_front: True

    do_rotation:False
    do_scale:False
    do_translation_y:False

    Image
        id: mouse_im
        size: 100, 100 / self.image_ratio
        source: 'cursor-pink.png'

"""

class MyTextInput(TextInput):
    pass

class MyMouse(Scatter):
    def __init__(self, **kwargs):
        Window.bind(mouse_pos=self.on_mouse_pos)
        Window.bind(on_cursor_leave=self.on_cursor_leave)
        Window.bind(on_cursor_enter=self.on_cursor_enter)
        super(MyMouse, self).__init__(**kwargs)

    def on_touch_down(self, *touch):
        return

    def on_mouse_pos(self, *args):
        x,y = args[1]
        self.pos = [x,y-self.mouse_im_size[1]]

    def on_cursor_leave(self, *args):
        App.get_running_app().root.ids.themouse.opacity = 0

    def on_cursor_enter(self, *args):
        App.get_running_app().root.ids.themouse.opacity = 1


class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)

MyApp().run()

我将themouse ID添加到了MyMouse小部件中。

答案 1 :(得分:0)

这是另一种方法,但是它需要在基本布局周围加上边框:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.scatter import Scatter
from kivy.core.window import Window
#Window.show_cursor = False

KV = """
FloatLayout
    canvas.before:
        Color:
            rgba: (1, 0, 0, 1)
        Rectangle:
            size: self.size
            pos: self.pos
    FloatLayout
        id: mainlayout
        size_hint: (None, None)
        #pos: (50, 50)
        #pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        canvas.before:
            Color:
                rgba: (0, 1, 0, 1)
            Rectangle:
                size: self.size
                pos: self.pos
        BoxLayout
            size_hint: (1.0, 0.2)
            pos_hint: {'center_x': 0.5, 'top': 1.0}
            MyTextInput
        StencilView:
            size_hint: (1.0, 1.0)
            pos_hint: {'x': 0, 'y': 0}
            MyMouse
                id: themouse


<MyTextInput>:
    font_size: 40
    text: 'Some text'

<MyMouse>:
    mouse_im_size: mouse_im.size
    auto_bring_to_front: True

    do_rotation:False
    do_scale:False
    do_translation_y:False

    Image
        id: mouse_im
        size: 100, 100 / self.image_ratio
        source: 'cursor-pink.png'

"""

class MyTextInput(TextInput):
    pass

class MyMouse(Scatter):
    def __init__(self, **kwargs):
        Window.bind(mouse_pos=self.on_mouse_pos)
        super(MyMouse, self).__init__(**kwargs)

    def on_touch_down(self, *touch):
        return

    def on_mouse_pos(self, *args):
        x,y = args[1]
        self.pos = [x,y-self.mouse_im_size[1]]

class MyApp(App):
    def build(self):
        self.mainlayout = None
        self.mymouse = None
        self.root = Builder.load_string(KV)
        Window.bind(size=self.do_size)
        Clock.schedule_once(self.do_size)

    def do_size(self, *args):
        if self.mainlayout is None:
            self.mainlayout = self.root.ids.mainlayout
        if self.mymouse is None:
            self.mymouse = self.root.ids.themouse

        self.mainlayout.size = (self.root.width - 2.0 * self.mymouse.mouse_im_size[0], self.root.height - 2.0 * self.mymouse.mouse_im_size[1])
        self.mainlayout.pos = self.mymouse.mouse_im_size

MyApp().run()

这使用StencilView将光标的图形剪切到man布局的内部。