簡體   English   中英

Kivy on Raspberry:光標位於窗口之外

[英]Kivy on Raspberry: Cursor is outside of window

我正在使用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__() ,將on_cursor_leave事件綁定到回調方法,例如cursor_leave()
  2. cursor_leave()方法中使用Window.grab_mouse() 有關詳細信息,請參閱示例。

片段

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)

窗口»grab_mouse()

 grab_mouse() 

抓住鼠標 - 所以不會離開窗口

注意

此功能需要SDL2窗口提供程序。

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

好的,我解決了這個問題。 這不是我最喜歡的解決方案,但它有效:

# Set mouse back to visible window when leaving 1920x1080
def mouse_pos(self, window, pos):
    try:
        #pos_width = int(pos[0])
        #pos_height = int(pos[1])

        if int(pos[0]) <= 0:                # Mouse outside, LEFT
            window.mouse_pos = (5, int(pos[1]))
        if int(pos[0]) > self.win_width:    # Mouse outside, RIGHT
            window.mouse_pos = (1900, int(pos[1]))
        if int(pos[1]) < 0:                 # Mouse outside, BOTTOM
            window.mouse_pos = (int(pos[0]), 20)
        if int(pos[1]) > self.win_height:   # Mouse outside, TOP
            window.mouse_pos = (int(pos[0]), 1060)

光標離開窗口時會移回光標。

這時我的方法是:

class WSRMEGUI(BoxLayout):
    ...

    def __init__(self, **kwargs):
        super(WSRMEGUI, self).__init__(**kwargs)
        Window.bind(mouse_pos=self.mouse_pos)

    def mouse_pos(self, window, pos):
        poswidth = int(pos[0])
        posheight = int(pos[1])
        winwidth = int(window.width)
        winheight = int(window.height)


        if poswidth <= 0:
            Window.grab_mouse()
        if poswidth > winwidth:
            Window.grab_mouse()
        if posheight < 0:
            Window.grab_mouse()
        if posheight > winheight:
            Window.grab_mouse()

所以如果光標離開窗口

Window.grab_mouse()

每次調用但沒有任何影響......有沒有辦法在離開允許區域時將光標設置回窗口?

暫無
暫無

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

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