Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the mouse pointer color in Tkinter?
Tkinter is a standard Python library for developing GUI-based applications. We can change the properties of its widgets by using the built-in functions and methods. In some applications, the properties affect the mouse pointer as well.
Tkinter provides us a way to change the mouse pointer color in the window. To configure the mouse pointer color, we can specify the cursor value with (cursor type and its color). For example, to change the cursor color in a label widget, we can specify the value as cursor="plus #aab1212" where "plus" defines the cursor type and #aab1212 is the Hex value of the color.
Syntax
widget_name = Widget(parent, cursor="cursor_type color", other_options)
Where cursor_type can be plus, dot, arrow, hand2, etc., and color can be a color name or hex value.
Example − Changing Mouse Pointer Color
Here's how to create widgets with different colored cursors ?
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
win.title("Mouse Pointer Color Example")
# Add label widget with red plus cursor
label = Label(win, text="Hover here for RED PLUS cursor",
cursor="plus red", font=('Arial', 16),
bg="lightblue", pady=10)
label.pack(pady=20)
# Add button with blue dot cursor
button1 = Button(win, text="Hover here for BLUE DOT cursor",
cursor="dot blue", font=('Arial', 14))
button1.pack(pady=10)
# Add button with green hand cursor
button2 = Button(win, text="Hover here for GREEN HAND cursor",
cursor="hand2 green", font=('Arial', 14))
button2.pack(pady=10)
# Add label with hex color cursor
label2 = Label(win, text="Hover here for PURPLE ARROW cursor",
cursor="arrow #800080", font=('Arial', 14),
bg="lightyellow", pady=10)
label2.pack(pady=10)
win.mainloop()
Common Cursor Types
| Cursor Type | Description | Example Usage |
|---|---|---|
arrow |
Standard pointer arrow | cursor="arrow red" |
hand2 |
Pointing hand | cursor="hand2 blue" |
plus |
Cross/plus symbol | cursor="plus green" |
dot |
Small dot | cursor="dot #FF5733" |
watch |
Loading/waiting cursor | cursor="watch black" |
Using Hex Color Values
You can also specify colors using hex values for more precise color control ?
from tkinter import *
win = Tk()
win.geometry("500x200")
win.title("Hex Color Cursors")
# Using hex colors
label1 = Label(win, text="Orange cursor (#FF8C00)",
cursor="plus #FF8C00", font=('Arial', 12))
label1.pack(pady=15)
label2 = Label(win, text="Purple cursor (#9932CC)",
cursor="hand2 #9932CC", font=('Arial', 12))
label2.pack(pady=15)
win.mainloop()
Conclusion
Tkinter allows easy customization of mouse pointer colors using the cursor parameter with format "type color". You can use standard color names or hex values for precise control, enhancing the visual appeal and user experience of your GUI applications.
