2

I'm trying to create a custom cursor in a Kivy app. I've looked at approaching it from the angle of tying the position of a Kivy widget to the cursor position. However, this method is extremely finicky and not very clean. If worst comes to worst I can stick with that approach, but I was curious if there is a way to create a custom cursor within Python itself.

I've looked at countless examples of creating a custom cursor in Python and all of the ones I have found require destroying and recreating the actual windows cursor. Every example I have found strongly discourages this approach because if you quit out of your program before recreating the cursor, then it obviously doesn't recreate itself and you're left with the custom cursor (which CAN be manually reset, but my program is for customer use). I am aware that you can set the cursor in Python to be a crosshair, a hand and a select variety of cursors, but I would like to use an actual image for the cursor.

Following is an example of the code that destroys and recreates your cursor (the method that is recommended to avoid). This snippet of (working) code can be found in the answer to How to change cursor image in Python .

hold = win32gui.LoadImage(0, 32512, win32con.IMAGE_CURSOR, 
                          0, 0, win32con.LR_SHARED )
hsave = ctypes.windll.user32.CopyImage(hold, win32con.IMAGE_CURSOR, 
                                       0, 0, win32con.LR_COPYFROMRESOURCE)
hnew = win32gui.LoadImage(0, 'file.cur', 
                             win32con.IMAGE_CURSOR, 0, 0, win32con.LR_LOADFROMFILE);
ctypes.windll.user32.SetSystemCursor(hcursor, 32512)
time.sleep(5)

#restore the old cursor
ctypes.windll.user32.SetSystemCursor(hsave, 32512)

Does my answer lie in an adaptation of the above code? Or is there a better approach altogether? I know it has to be possible to set a custom cursor in Python, I'm just pulling up blanks every source I go to.

P.S. I apologize in advance about my question, this is my first one on Stack haha.

  • 1
    https://stackoverflow.com/questions/53851589/kivy-undesirable-behavior-of-custom-mouse-cursor-when-crossing-left-or-top-edge Try this – clubby789 Aug 06 '19 at 15:58
  • 1
    Thank you @JammyDodger. This was actually one of the best results that I had found, but it is the one that relies on using the Kivy mouse event. I've tested it out before and it doesn't run as smoothly as I would like. No worries though, I should be better next time at explaining everything I have tried and what it is that I'm currently using. – py_physicist Aug 07 '19 at 12:02
  • 2
    Kivy has no special support for this. I think SDL2 has an api for simple grayscale cursors, but I don't think we expose it. There's also an api for a few preset cursor types (hourglass for waiting cursor etc.), which we might expose, but doesn't support arbitrary images. If you want something other than those things, the intended solution is that you draw it yourself. – inclement Aug 07 '19 at 17:40
  • 1
    You're right @inclement , SDL2 does have an unexposed api. I've used the existing api for preset cursor types. I was using the preset cursor for crosshair, but I was instructed to give the crosshair a different color. I found out that was not possible (at least to my knowledge), so decided to make a custom cursor instead. I'm surprised that Python doesn't have a way to make it custom because languages like Excel VBA do. Thanks for the info, if there's no other solution I guess I'll stick with drawing it myself. – py_physicist Aug 07 '19 at 18:51

0 Answers0