Changing the Mouse Cursor in Tkinter

PythonTkinterGUI-Programming

Tkinter is a GUI-based Python library which is used to develop various types of functional and GUI-based applications. It provides lots of functions and methods that can be used to provide extensibility and various features while developing an application.

In this article, we will see how we can change the mouse cursor while hovering on a button in the tkinter frame using the cursor property. There are plenty of cursor maps available in tkinter’s button library that provide different visuals for the end-user. Some of the cursor in the library are,

"arrow"

"circle"

"clock"

"cross"

"dotbox"

"exchange"

"fleur"

"heart"

"heart"

"man"

"mouse"

"pirate"

"plus"

"shuttle"

"sizing"

"spider"

"spraycan"

"star"

"target"

"tcross"

"trek"

"watch"

Let us first create some buttons and then we will apply some of these cursors on the mouse pointer.

Example

from tkinter import *
#Create an instance of window or frame
win= Tk()
#Set the geometry
win.geometry("700x600")
win.resizable(0,0)
win.config(cursor= "fleur")
#Let us create a text label
Label(win, text= "Hover on each of these buttons", font=('Poppins', 20)).pack(pady=20)

#Create some buttons with cursor property
b1= Button(win, text= "Star",cursor="star")
b1.pack(pady=20)
b2= Button(win, text= "Arrow",cursor="arrow")
b2.pack(pady=20)
b3= Button(win, text= "Circle",cursor="circle")
b3.pack(pady=20)
b4= Button(win, text= "Clock",cursor="clock")
b4.pack(pady=20)
b5= Button(win, text= "Heart",cursor="heart")
b5.pack(pady=20)
b6= Button(win, text= "Man",cursor="man")
b6.pack(pady=20)
b7= Button(win, text= "Mouse",cursor="mouse")
b7.pack(pady=20)

#Keep Running the window

win.mainloop()

Output

Running the above code will create different buttons with different mouse pointer shapes.

raja
Published on 04-Mar-2021 13:31:24
Advertisements