Python Tkinter section

Python Tkinter Cursors

Post by Amina Delali, April 23th, 2023

Description

For every widget in Tkinter, you can set or change the mouse pointer that appears when you hover over the widget. You do that by setting the "cursor" property of the widget. You can select one of the many available cursors shapes that are already defined.

In the code below we do 3 things:

  • we display on each page 4x5 cursors names arranged in 4 rows and 5 columns. We use for that the label widget
  • under the name, we display the image of the corresponding cursor in a Canvas widget. When you hover over the Canva, the corresponding cursor will appear as well.
  • in the page there are navigational buttons that allow you to display the next set of cursors (or the previous ones) depending on the button. To do this, each one of them will call the updateCursors function (displayed here), that assign for each Canvas the new cursors. It also create the image corresponding to that cursor, and display it inside that canvas. It also, change the text of the label above the canvas, so it will display the name of the selected cursor.

You can download the images used for that code from here. You images must be in the "cursors" folder, in the same folder of the code file. The code must also be run from that folder location.



The Code

from tkinter import *
from PIL import ImageTkImage

main_window=Tk()
main_window.title("Python Tkinter Cursors")
bgColor="#24252B"
fgColor ="#ffffff"
cursors = [
["arrow",
 "circle",
 "clock",
 "cross",
 "dotbox"],
["exchange",
"fleur",
"heart",
"man",
"mouse"],
["pirate",
"plus",
"shuttle",
"sizing",
"spider"],
["spraycan",
"star",
"target",
"tcross",
"trek"],
["watch",
"based_arrow_down",
"middlebutton",
"based_arrow_up",
"boat"],
["pencil",
"bogosity",
"bottom_left_corner",
"bottom_right_corner",
"question_arrow",],
["bottom_side",
"right_ptr",
"bottom_tee",
"box_spiral",
"right_tee"],
["center_ptr",
"rightbutton",
"rtl_logo",
"sailboat",
"coffee_mug"],
["sb_down_arrow",
"cross_reverse",
"crosshair",
"sb_h_double_arrow" ,
"sb_left_arrow",],
["sb_right_arrow",
"diamond_cross",
"sb_up_arrow",
"dot",
"sb_v_double_arrow"],
["gumby",
"gobbler",
"icon",
"double_arrow",
"draft_large"],
["draft_small",
"draped_box",
"top_left_arrow",
"top_left_corner",
"hand1"],
["top_right_corner",
"hand2",
"top_side",
"top_tee",
"iron_cross"],
["ul_angle",
"left_ptr",
"umbrella",
"left_side",
"ur_angle"],
["left_tee",
"leftbutton",
"xterm",
"ll_angle",
"X_cursor",],
["lr_angle",
"lr_angle",
"lr_angle",
"lr_angle",
"lr_angle"]
]
    
main_window.geometry('600x550+500+50'
main_window.configure(bg="#24252B")

toAdd=0
labels=[]
canvas = []

for in range (4):
    labels.append([])
    canvas.append([])
    for in range (5):
        lab Label(main_window,text=cursors[i][j],
                bg=bgColor,fg=fgColor)
        lab.grid(row=i*2,column=j)
        labels[i].append(lab)
        
        can Canvas(main_windowwidth=80height=80,
                        cursor=cursors[i][j],
                        bg=bgColor)
        imageFile Image.open("cursors/"+cursors[i][j]+".png")
        imageFile ImageTk.PhotoImage(imageFile)
        can.image imageFile
        can.create_image(80/280/2anchor=CENTERimage=imageFiletags="bg_img")
        can.grid(row=i*2+1,column=j,padx=8,pady=8)
        canvas[i].append(can)
        
def updateCursors():
    global labels
    global canvas
    for in range (4):
        for in range (5):
            labels[i][j].configure(text=cursors[i+toAdd][j])
            canvas[i][j].configure(cursor=cursors[i+toAdd][j])
            imageFile Image.open("cursors/"+cursors[i+toAdd][j]+".png")
            imageFile ImageTk.PhotoImage(imageFile)
            canvas[i][j].image imageFile
            canvas[i][j].create_image(80/280/2
                    anchor=CENTERimage=imageFiletags="bg_img")

def nextPage():
    global toAdd
    if toAdd<12:
        toAdd=toAdd+4
        updateCursors()


def prevPage():
    global toAdd
    if toAdd>0:
        toAdd=toAdd-4
        updateCursors()     

def firstPage():
    global toAdd
    toAdd=0     
    updateCursors()   

def lastPage():
    global toAdd
    toAdd=12     
    updateCursors()           

firstBut Button(main_window,text="First",bg="#bd9b16",
                 command=firstPage)
firstBut.grid(row=8,column=0,padx=8,pady=8,sticky=E)
lastBut Button(main_window,text="Last",bg="#bd9b16",
                 command=lastPage)
lastBut.grid(row=8,column=1,padx=8,pady=8,sticky=W)


prevBut Button(main_window,text="Previous",bg="#bd9b16",
                 command=prevPage)
prevBut.grid(row=8,column=4,padx=8,pady=8,sticky=E)
nextBut Button(main_window,text="Next",bg="#bd9b16",
                 command=nextPage)
nextBut.grid(row=8,column=5,padx=8,pady=8,sticky=E)


main_window.mainloop()

The Output

Something to say?

If you want to add something about this post, please feel free to do it by commenting below 🙂.