简体   繁体   English

Python tkinter 如何将 cursor 设置为图像

[英]Python tkinter how can I set the cursor as an image

I want the cursor to be an image when hovering over my canvas, when i run this it gives me 'bad cursor spec "pyimage1"' error.当我将鼠标悬停在我的 canvas 上时,我希望 cursor 成为图像,当我运行它时,它给了我“糟糕的 cursor 规范“pyimage1”错误。 Is it possible?可能吗?

from tkinter import*

root = Tk()
root.geometry('500x500+0+0')

custom_cursor = PhotoImage(file='custom_cursor.png')

canvas = Canvas(root, height=50, width=50, bg='black', cursor=custom_cursor)
canvas.pack()

root.mainloop()

No, it is not possible to use an image as a cursor.不可以,不能将图像用作 cursor。 At least, not directly.至少,不是直接的。 The cursor option must be one of the following: cursor选项必须是以下选项之一:

  • the name of a standard X11 cursor (see here for a list of valid cursor names)标准 X11 cursor 的名称(有关有效 cursor 名称的列表,请参见此处
  • the path to a file in the X11 or X10 cursor format X11 或 X10 cursor 格式的文件的路径
  • on Windows only, the path to a.ani or.cur file仅在 Windows 上,a.ani 或.cur 文件的路径

The definitive description of the cursor option is on the tcl/tk man page for Tk_GetCursor cursor选项的最终描述在Tk_GetCursor的 tcl/tk 手册页上

Since you are using a canvas, you can simulate a cursor with an image by drawing a normal image and updating it's position whenever the cursor moves, though it would lag very slightly being the real cursor. Since you are using a canvas, you can simulate a cursor with an image by drawing a normal image and updating it's position whenever the cursor moves, though it would lag very slightly being the real cursor.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM