Kivy:越过应用程序窗口的左边缘或上边缘时,自定义鼠标光标的不良行为 [英] Kivy: undesirable behavior of custom mouse cursor when crossing left or top edge of app window
问题描述
我想用猕猴桃做一个自定义的鼠标光标. 这是我目前所拥有的:
I want to make a custom mouse cursor in kivy. This is what I have at the moment:
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()
问题是,当我将鼠标移到应用程序的左边缘或上边缘之外时,光标图像仍保留在应用程序内,并且我希望鼠标图像消失,就像我将鼠标移到右边缘或下边缘之外时一样
the problem is that when I move the mouse beyond the left or upper edge of application, the cursor image remains within the app, and I want the mouse image to disappear just like when I move the mouse beyond the right or lower edge.
问题似乎是on_mouse_pos()
仅在鼠标位于窗口内时有效.
It seems the problem is that on_mouse_pos()
only works when the mouse is inside the window.
我找到了一种方法来获取鼠标的位置当它在窗口之外时,但我不知道如何在任务中使用它.也许有更好的方法可以做到这一点.
I found a way to get the position of the mouse when it is outside the window, but I do not know how this can be used in my task. And maybe there is a better way to do this.
推荐答案
您可以通过使用Window
事件on_cursor_enter
和on_cursor_leave
并通过使用opacity
属性使光标可见/不可见来实现此目的:
You can accomplish this by using the Window
events on_cursor_enter
and on_cursor_leave
and making the cursor visible/invisible by using the opacity
property:
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
小部件中.
I added the themouse
id to the MyMouse
widget to accomplish this.
这篇关于Kivy:越过应用程序窗口的左边缘或上边缘时,自定义鼠标光标的不良行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!