Python Tkinter如何将光标设置为图像 [英] Python tkinter how can I set the cursor as an image

查看:95
本文介绍了Python Tkinter如何将光标设置为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标悬停在画布上时,我希望光标为图像,运行此程序时会出现错误的光标规范"pyimage1""错误.有可能吗?

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()

推荐答案

否,不能将图像用作光标.至少不是直接. cursor 选项必须为以下之一:

No, it is not possible to use an image as a cursor. At least, not directly. The cursor option must be one of the following:

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

cursor 选项的确切说明位于

The definitive description of the cursor option is on the tcl/tk man page for Tk_GetCursor

由于您使用的是画布,因此可以通过绘制普通图像并在光标移动时更新其位置来模拟带有图像的光标,尽管与实际光标相比,滞后时间会稍短.

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.

这篇关于Python Tkinter如何将光标设置为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆