Open In App

Changing the Mouse Cursor – Tkinter

Prerequisite: Python GUI – tkinter

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python.



In this article, we will learn, how to change the mouse cursor in Tkinter Using Python.

There are about 20 cursors that can be found in Tkinter:



Step1: Create Normal Tkinter window and add Button




# Import Required Library
from tkinter import *
  
# Create Object
root = Tk()
  
# Set geometry
root.geometry("400x400")
  
Button(root,text="Button",font=("Helvetica 15 bold")).pack()
  
# Execute Tkinter
root.mainloop()

Output:

Step2: Add cursor in button

For adding a cursor in Button, use cursor attributes.

Button(root,text="Button",font=("Helvetica 15 bold"),cursor="star").pack()

Use all cursors

Below is the GUI looks like:-

Below is the implementation:




# Import Required Library
from tkinter import *
  
# Create Object
root = Tk()
  
# Set geometry
root.geometry("200x530")
  
# List of cursors
cursors =[
        "arrow",
        "circle",
        "clock",
        "cross",
        "dotbox",
        "exchange",
        "fleur",
        "heart",
        "man",
        "mouse",
        "pirate",
        "plus",
        "shuttle",
        "sizing",
        "spider",
        "spraycan",
        "star",
        "target",
        "tcross",
        "trek"
]
  
  
  
# Iterate through all cursors
for cursor in cursors:
    Button(root,text=cursor,cursor=cursor).pack()
  
  
# Execute Tkinter
root.mainloop()

Output:-

For making the cursor globally, use config() method.




# Import Required Library
from tkinter import *
  
# Create Object
root = Tk()
  
# Set geometry
root.geometry("400x400")
  
# Cursor
root.config(cursor="star")
  
# Execute Tkinter
root.mainloop()

Output:-


Article Tags :