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.
- A mouse cursor is treated as an indicator, which is used to show the current position of the user position on a computer. It is also called a pointer.
- Every mouse cursor has its own use. For example, for dragging the image we use fleur mouse cursor, for rotating the text we use exchange mouse cursor etc.
There are about 20 cursors that can be found in Tkinter:
- arrow
- circle
- clock
- cross
- dotbox
- exchange
- fleur
- heart
- man
- mouse
- pirate
- plus
- shuttle
- sizing
- spider
- spraycan
- star
- target
- tcross
- trek
Step1: Create Normal Tkinter window and add Button
Python3
from tkinter import *
root = Tk()
root.geometry( "400x400" )
Button(root,text = "Button" ,font = ( "Helvetica 15 bold" )).pack()
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
- Make a list, which contains all cursors
- Iterate through all cursors.
Below is the GUI looks like:-
Below is the implementation:
Python3
from tkinter import *
root = Tk()
root.geometry( "200x530" )
cursors = [
"arrow" ,
"circle" ,
"clock" ,
"cross" ,
"dotbox" ,
"exchange" ,
"fleur" ,
"heart" ,
"man" ,
"mouse" ,
"pirate" ,
"plus" ,
"shuttle" ,
"sizing" ,
"spider" ,
"spraycan" ,
"star" ,
"target" ,
"tcross" ,
"trek"
]
for cursor in cursors:
Button(root,text = cursor,cursor = cursor).pack()
root.mainloop()
|
Output:-
For making the cursor globally, use config() method.
Python3
from tkinter import *
root = Tk()
root.geometry( "400x400" )
root.config(cursor = "star" )
root.mainloop()
|
Output:-
Last Updated :
24 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...