Python 3 – Tkinter Cursors
Tkinter is a GUI toolkit for Python. Tkinter provides several useful widgets such as Toplevel, Frame, Label, Button, Checkbutton, Radiobutton, Entry, Text, Canvas, etc. Cursors play an important role in the graphical user interface of Tkinter. Cursors are the mouse pointers used by the operating system.
Tkinter Cursors are used to change the default cursor used by the operating system. Cursors can be changed depending on user requirements. Python 3 provides several cursor types to choose from.
Predefined Cursors
Tkinter provides several predefined cursor types. The cursor types are:
Arrow, circle, clock, cross, dot, exchange, fleur, heart, man, mouse, pirate, plus, pointing, question, right, sailboat, shibboleth, spider, spraycan, star, target, tcross, trek, umbrella, watch.
The following code is an example of predefined cursors.
from tkinter import *
master = Tk()
# Available cursors
cursors = [
"arrow",
"circle",
"clock",
"cross",
"dot",
"exchange",
"fleur",
"heart",
"man",
"mouse",
"pirate",
"plus",
"pointing",
"question",
"right",
"sailboat",
"shibboleth",
"spider",
"spraycan",
"star",
"target",
"tcross",
"trek",
"umbrella",
"watch"
]
for cursor in cursors:
Button(master, text=cursor, cursor=cursor).pack()
master.mainloop()
This code creates a window with buttons for each predefined cursor. When a button is clicked, the mouse cursor is changed to that cursor.
Custom Cursors
In addition to the predefined cursors, Tkinter also allows custom cursors. Custom cursors can be created by providing an image to be used as the cursor. The image must be in the format of a Portable Bitmap (PBM) or Portable Pixmap (PPM) file.
The following code demonstrates how to use a custom cursor.
from tkinter import *
master = Tk()
# Custom cursor
ppmdata = """
#define arrow_width 32
#define arrow_height 32
static unsigned char arrow_bits[] = {
0xfe, 0x03, 0xfe, 0x03, 0x7e, 0x00, 0x3e, 0x00, 0x1e, 0x00, 0x0e, 0x00,
0x06, 0x00, 0xfe, 0x7f, 0xfe, 0x7f, 0x06, 0x00, 0x0e, 0x00, 0x1e, 0x00,
0x3e, 0x00, 0x7e, 0x00, 0xfe, 0x03, 0xfc, 0x07
};
"""
cursor_data = (32, 32), (16, 16), bytes.fromhex("ffffff"), bytes.fromhex("000000"), bytes.fromhex(ppmdata.strip())
my_cursor = Cursor(master, cursor_data)
Frame(master, width=100, height=100, cursor=my_cursor).pack()
master.mainloop()
This code creates a custom cursor using the data in the ppmdata
variable. The cursor data is specified in XPM format. The image is a black arrow on a white background. The image is 32 pixels wide and 32 pixels high.
Conclusion
Cursors play an important role in the graphical user interface of Python 3 Tkinter. Cursors are used to change the default cursor used by the operating system. Tkinter provides several predefined cursor types and also allows custom cursors to be created by providing an image to be used as the cursor. Python developers can use Pyinstaller, cx_Freeze or Py2exe to create standalone executable files of their applications. Cursors can be changed depending on user requirements and can provide a better user experience.