python中tkinter较完整的鼠标样式cursor值

什么是cursor

 

cursor是指(计算机荧光屏上的)光标,游标; 

在tkinter中的Frame, Label, Button, Radiobutton, Checkbutton, Entry, Message, LabelFrame, Scrollbar, Listbox, Scale, Canvas等组件设置中,cursor是**kw参数之一,通过赋值为字符串可改变鼠标经过组件的样式。

 

使用方法

 前后大部分不变,后面不做重复,只需更改第3行即可

from tkinter import *  # 导入tkinter包
root = Tk(className="这是一个窗口")  # 创建一个窗口
Frame(root, width=100, height=100, cursor="hand2").pack()  # 创建一个Frame,使鼠标经过时呈现手形
mainloop()  # 显示窗口
Label(root, text="作者", cursor="hand2").pack()  # 创建一个Label,使鼠标经过时呈现手形
Button(root, text="是", cursor="xterm").pack()  # 创建一个Button,使鼠标经过时呈现工字形
Radiobutton(root, text="血小板397", cursor="hand2").pack()  # 创建一个Radiobutton,使鼠标经过时呈现手形
Checkbutton(root, text="原创", cursor="watch").pack()  # 创建一个Checkbutton,使鼠标经过时呈现环形
Entry(root, cursor="fleur").pack()  # 创建一个Entry,使鼠标经过时呈现移动形式

同时你也可以通过调用config()方法修改cursor的值,来修改鼠标经过的样式,例如:

root.config(cursor="wait")  # 通过config方法甚至可以直接更改根窗口的鼠标样式

 

列举相关的77种鼠标样式:

arrow, xterm, watch, hand2, question_arrow, sb_h_double_arrow, sb_v_double_arrow, fleur, crosshair, based_arrow_down, based_arrow_up, boat, bogosity, top_left_corner, top_right_corner, bottom_left_corner, bottom_right_corner, top_side, bottom_side, top_tee, bottom_tee, box_spiral, center_ptr, circle, clock, coffee_mug, cross, cross_reverse, diamond_cross, dot, dotbox, double_arrow, top_left_arrow, draft_small, draft_large, left_ptr, right_ptr, draped_box, exchange, gobbler, gumby, hand1, heart, icon, iron_cross, left_side, right_side, left_tee, right_tee, leftbutton, middlebutton, rightbutton, ll_angle, lr_angle, man, mouse, pencil, pirate, plus, rtl_logo, sailboat, sb_left_arrow, sb_right_arrow, sb_up_arrow, sb_down_arrow, shuttle, sizing, spider, spraycan, star, target, tcross, trek, ul_angle, umbrella, ur_angle, X_cursor

下面对常用的9种进行解释:

arrow: 箭头(默认的鼠标样式),  xterm: 文本编辑时(工字形),  watch = wait: 等待程序反应(圆圈形),

hand2: 点击按钮时(手形),   question_arrow: 帮助(箭头右下问号),

sb_h_double_arrow: 左右扩展窗口时,   sb_v_double_arrow: 上下扩展窗口时,

fleur: 移动窗口时,   crosshair: 十字准心

·动手试试:

执行以下代码,进一步了解这77种鼠标样式

import tkinter as t
def show(cursor, i):
    t.Label(f, text=cursor, cursor=cursor).grid(column=i // 20, row=i % 20)
f = t.Tk(className='Cursor')
cursorList = ['arrow', 'xterm', 'watch', 'hand2', 'question_arrow', 'sb_h_double_arrow', 'sb_v_double_arrow', 'fleur',
              'crosshair', 'based_arrow_down', 'based_arrow_up', 'boat', 'bogosity', 'top_left_corner',
              'top_right_corner', 'bottom_left_corner', 'bottom_right_corner', 'top_side', 'bottom_side', 'top_tee',
              'bottom_tee', 'box_spiral', 'center_ptr', 'circle', 'clock', 'coffee_mug', 'cross', 'cross_reverse',
              'diamond_cross', 'dot', 'dotbox', 'double_arrow', 'top_left_arrow', 'draft_small', 'draft_large',
              'left_ptr', 'right_ptr', 'draped_box', 'exchange', 'gobbler', 'gumby', 'hand1', 'heart', 'icon',
              'iron_cross', 'left_side', 'right_side', 'left_tee', 'right_tee', 'leftbutton', 'middlebutton',
              'rightbutton', 'll_angle', 'lr_angle', 'man', 'mouse', 'pencil', 'pirate', 'plus', 'rtl_logo', 'sailboat',
              'sb_left_arrow', 'sb_right_arrow', 'sb_up_arrow', 'sb_down_arrow', 'shuttle', 'sizing', 'spider',
              'spraycan', 'star', 'target', 'tcross', 'trek', 'ul_angle', 'umbrella', 'ur_angle', 'X_cursor']
for i in range(len(cursorList)):
    show(cursorList[i], i)
f.mainloop()

结果如图

python中tkinter较完整的鼠标样式cursor值_第1张图片

相关参考图片

python中tkinter较完整的鼠标样式cursor值_第2张图片

参考资料

[1].python中Tkinter的鼠标样式cursor(带图示):https://blog.csdn.net/FujLiny/article/details/115828058

[2].tkinter cursor: https://blog.csdn.net/m0_57901298/article/details/117326933

如有问题,欢迎在下方评论指出,谢谢

你可能感兴趣的:(python,python,tkinter,cursor)