簡體   English   中英

Kivy:越過應用程序窗口的左邊緣或上邊緣時,自定義鼠標光標的不良行為

[英]Kivy: undesirable behavior of custom mouse cursor when crossing left or top edge of app window

我想在獼猴桃中制作一個自定義鼠標光標。 這是我目前所擁有的:

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()僅在鼠標位於窗口內時才起作用。

找到了一種方法來獲取鼠標在窗口外部時的位置,但是我不知道如何將其用於任務中。 也許有更好的方法可以做到這一點。

您可以通過使用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小部件中以完成此操作。

這是另一種方法,但是需要在基本布局周圍加上邊框:

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布局的內部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM