Skip to content
Related Articles

Related Articles

Improve Article
How to change the color and symbol of the cursor in Tkinter?
  • Last Updated : 24 Jan, 2021

Tkinter is a standard GUI library for Python. It provides various widgets for GUI development. We can give background color to widgets as per choice. But, sometimes background color affects mouse pointer visibility.  In such cases, changing the cursor color makes the mouse pointer distinguishable. The color of the cursor can be changed by specifying the color with the cursor type. These values can be specified in the cursor attribute while creating the widget or can be specified using config() method for that widget. Cursor type and cursor color if not defined for widget inherits from the parent window. Cursor colors can be specified with their standard names or with hexadecimal RGB values. For example, cursor=”plus red” or cursor=”plus #FF0000″, will provide plus cursor icon with red color.

For some cursors, it is possible to provide 2 colors, fill color and border color. For example, cursor=”dot red blue”, will provide a dot cursor icon with red color bordered by blue color.

Steps:

  • Create a Tkinter window
  • Specify cursor icon and color for the window using cursor parameter
  • Specify cursor icon and color for other widgets while creating them or using config method for that widget.

The following program demonstrates the change in colors of the cursor and also a change in the cursor for top-level window and other widgets.

Python3






# Import library
import tkinter as tk
  
# Create Tkinter window
frame = tk.Tk()
frame.title('GFG Cursors')
frame.geometry('200x200')
  
# Specify dot cursor with blue color for frame
frame.config(cursor="dot blue")
  
# Specify various cursor icons with colors
# for label and buttons
tk.Label(frame, text="Text cursor",
         cursor="xterm #0000FF").pack()
  
tk.Button(frame, text="Circle cursor",
          cursor="circle #FF00FF").pack()
  
tk.Button(frame, text="Plus cursor",
          cursor="plus red").pack()
  
# Specify cursor icon and color using
# config() method
a = tk.Button(frame, text='Exit')
a.config(cursor="dot green red")
a.pack()
  
frame.mainloop()

Output:

Note: Changing cursor color in Tkinter is not supported in Windows. For available cursors list can refer here.

We can change text cursor color using insertbackground parameter for text widget. Following program demonstrates text cursor color change.

Python3




# Import library
import tkinter as tk
  
# Create top level window
frame = tk.Tk()
frame.title("Text Cursor")
frame.geometry('200x200')
  
# Create Text widget with "red" text cursor
inputtxt = tk.Text(frame, height=5, width=20
                   insertbackground="red")
inputtxt.place(x=20, y=20)
  
frame.mainloop()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :