python - 覆盆子上的 Kivy : Cursor is outside of window

标签 python kivy raspberry-pi3

我正在使用 Python3 和 Kivy 1.11.0dev 在 RaspberryPi 3 上开发 GUI。 GUI 正在工作,启动后它在全屏运行(据我所知,自 Kivy 1.9.x 以来,不可能将 Kivy 应用程序作为窗口运行)并且鼠标光标可见。现在唯一的问题是,如果用户向左、向右、向上或向下移动太远,则可以将鼠标移出可见区域。如果发生这种情况,就很难让光标回到可见区域。

我尝试了很多方法来防止这种情况发生,或者自动将光标带回到窗口,但没有成功。从类似的帖子中,我尝试了这样的事情:

Window.bind(on_cursor_leave=self.on_leave)

def on_leave(self, *args):
    if self.hold:
    print('Cursor leaved Window')
    # call api calls here

我也尝试过捕获鼠标

Window.bind(grab_mouse=self.on_leave)

有没有人有办法让光标离开后回到可见区域或设置光标无法离开的边框?

编辑:也许这个输出有帮助:

[INFO   ] [Logger      ] Record log in /home/pi/.kivy/logs/kivy_18-07-30_8.txt
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Kivy        ] v1.11.0.dev0, git-0471549, 20180720
[INFO   ] [Python      ] v3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1]
[INFO   ] [KivyMD      ] KivyMD version: 0.1.2
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: egl_rpi
[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
[INFO   ] [GL          ] Backend used <gl>
[INFO   ] [GL          ] OpenGL version <b'OpenGL ES 2.0'>
[INFO   ] [GL          ] OpenGL vendor <b'Broadcom'>
[INFO   ] [GL          ] OpenGL renderer <b'VideoCore IV HW'>
[INFO   ] [GL          ] OpenGL parsed version: 2, 0
[INFO   ] [GL          ] Shading version <b'OpenGL ES GLSL ES 1.00'>
[INFO   ] [GL          ] Texture max size <2048>
[INFO   ] [GL          ] Texture max units <8>
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
xclip version 0.12
Copyright (C) 2001-2008 Kim Saunders et al.
Distributed under the terms of the GNU GPL
[INFO   ] [Clipboard   ] Provider: xclip
[INFO   ] [CutBuffer   ] cut buffer support enabled
[INFO   ] [ProbeSysfs  ] device match: /dev/input/event0
[INFO   ] [HIDInput    ] Read event from </dev/input/event0>
[INFO   ] [ProbeSysfs  ] device match: /dev/input/event1
[INFO   ] [HIDInput    ] Read event from </dev/input/event1>
[INFO   ] [Base        ] Start application main loop
[INFO   ] [HIDMotionEvent] using <b'MLK USB Composite Device\x00                                                                                                                                                                                                                                       '>
[INFO   ] [HIDMotionEvent] using <b'MLK USB Composite Device\x00  

最佳答案

解决方案

  1. 在构造函数中__init__() ,绑定(bind)on_cursor_leave事件到回调方法,例如cursor_leave() .
  2. 使用Window.grab_mouse()里面cursor_leave()方法。详情请参阅示例。

代码片段

class GrabMouseDemo(Label):

    def __init__(self, **kwargs):
        super(GrabMouseDemo, self).__init__(**kwargs)
        Window.bind(mouse_pos=self.mouse_pos)
        Window.bind(on_cursor_leave=self.cursor_leave)

Window » grab_mouse()

grab_mouse()

Grab mouse - so won’t leave window

Note

This feature requires the SDL2 window provider.

示例

from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window


class GrabMouseDemo(Label):

    def __init__(self, **kwargs):
        super(GrabMouseDemo, self).__init__(**kwargs)
        Window.bind(mouse_pos=self.mouse_pos)
        Window.bind(on_cursor_leave=self.cursor_leave)

    def mouse_pos(self, window, pos):
        self.text = str(pos)

    def cursor_leave(self, window):
        print("cursor_leave:")
        Window.grab_mouse()


class TestApp(App):
    title = "Kivy Grab Mouse Demo"

    def build(self):
        return GrabMouseDemo()


if __name__ == "__main__":
    TestApp().run()

输出

Img01

关于python - 覆盆子上的 Kivy : Cursor is outside of window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51558019/

相关文章:

python - Beautiful Soup 的空元素错误

Python:反转字符串

python - 如何将kivy项目转换为android的apk

python - 如何收集文本输入数据作为变量以在 If 语句中使用

python - 使用数组对象计算 Spark RDD 中的不同文本

python - 如何在 pytest 中传递参数化方法的预处理变量?

python - 我如何将一个小部件添加到 FloatLayout 内 ScrollView 内的 StackLayout 中?

uwp - Windows 10 IoT Core Startup App Headed vs Headless

elasticsearch - Elastic Search 6.x不断失败

java - rfcomm 无法在树莓派 3 上与 tomcat7 配合使用