WoJ
WoJ

Reputation: 29987

How to hide or disable the mouse pointer in Tkinter?

I have a fullscreen Tkinter Python application which does not need the mouse -- a simplified version is below. It opens fullscreen and activates a text widget upon pressing F1.

import Tkinter as tk

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.root.attributes('-fullscreen', True)
        self.root.configure(background='red')
        self.root.bind('<F1>', self.opennote)
        self.root.bind('<F2>', self.closenote)
        self.root.bind('<F3>', self.quit)
        l = tk.Label(text="some text here")
        l.pack()
        self.root.mainloop()

    def opennote(self, event):
        self.n = tk.Text(self.root, background='blue')
        self.n.pack()

    def closenote(self, event):
        self.n.destroy()

    def quit(self, event):
        self.root.destroy()

App()

When launched, the mouse pointer is not visible. It becomes visible, though, after initiating the Text widget, and then stays (changing shape between the text frame and the rest of the screen).

I found several articles about how to hide a mouse cursor (by using cursor='' in parameters) but I did not find anything which would work for the mouse pointer across the widgets.

Is it possible to completely hide (or disable) the mouse pointer in Tkinter?

(a question on how to set the mouse position helped me to move this cursor away by issuing a self.root.event_generate('<Motion>', warp=True, x=self.root.winfo_screenwidth(), y=self.root.winfo_screenheight()). This is not a solution but at least the pointer does not jump in one's face from the middle of the screen)

Upvotes: 16

Views: 24415

Answers (2)

dhruvvyas90
dhruvvyas90

Reputation: 1205

I guess,

root.config(cursor="none") should work.

Upvotes: 38

Fiver
Fiver

Reputation: 10167

The closest I can come is to create a Frame and set the cursor to 'none', but it still has an issue of requiring the cursor to leave and re-enter the app window, at least on my machine (Mac OS X Mavericks). Maybe someone else can figure out how to trigger the cursor to disappear when the application loads, but here's the code I have so far:

import Tkinter as tk


class App():
    def __init__(self):
        self.root = tk.Tk()
        self.root.attributes('-fullscreen', True)
        self.main_frame = tk.Frame(self.root)
        self.main_frame.config(background='red', cursor='none')
        self.main_frame.pack(fill=tk.BOTH, expand=tk.TRUE)
        self.root.bind('<F1>', self.opennote)
        self.root.bind('<F2>', self.closenote)
        self.root.bind('<F3>', self.quit)
        l = tk.Label(self.main_frame, text="some text here")
        l.pack()
        self.root.mainloop()

    def opennote(self, event):
        self.n = tk.Text(self.main_frame, background='blue')
        self.n.pack()

    def closenote(self, event):
        self.n.destroy()

    def quit(self, event):
        self.root.destroy()

App()

Upvotes: 6

Related Questions