mmkw43
Posts: 553
Joined: Tue Dec 24, 2013 6:18 pm

Tkinter -- cursor invisible

Mon Jan 25, 2016 12:48 am

Been searching without luck. Could someone possibly point me to info on how to make the cursor invisible in Tkinter. I want to make it disappear and then reappear with a mouse movement if possible. Thanks much.

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: Tkinter -- cursor invisible

Mon Jan 25, 2016 8:28 am

install unclutter
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: Tkinter -- cursor invisible

Mon Jan 25, 2016 1:10 pm

to make the cursor disappear

Code: Select all

root.config(cursor='none')
to make the cursor reappear

Code: Select all

root.config(cursor='')

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: Tkinter -- cursor invisible

Mon Jan 25, 2016 1:45 pm

using the above code heres an example using movement over 2 seconds

Code: Select all

import Tkinter as tk
import time
import threading
root = tk.Tk()

x = 0
y = 0


def motion(event):
    global  x,y
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

root.bind('<Motion>', motion)


def cursor():
    while 1:
        xx = x
        yy = y
        time.sleep(2)
        if xx == x and yy == y:
            root.config(cursor='none')
            root.update_idletasks()
        else:
            root.config(cursor='')
            root.update_idletasks()

t = threading.Thread(target=cursor)
t.daemon = True
t.start()


root.mainloop()
with this code the 2 seconds delay applies on the curser reappear as well as the disappear, this would be relatively easy to overcome. the other thing is the mouse has to be in the tkinter window for it to work..

I was able to find the info to compile this by using google! first search for tkinter mouse cursor, second search for python mouse movement.

User avatar
RogerW
Posts: 291
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Tkinter -- cursor invisible

Mon Jan 25, 2016 3:07 pm

You can call config for any window. This code hides the cursor for the main window but you could hide it for a frame or individual widget. The cursor is still there and does work but you cannot see it.

Code: Select all

# Trythings.py
# written by Roger Woollett
import tkinter as tk
         
class Frame(tk.Frame):
    # class inherits frame and therefor IS a Frame object
    def __init__(self,master,*args,**kwargs):
        tk.Frame.__init__(self,master,*args,**kwargs)
                
        tk.Button(self,text = 'Press',command = self.on_button).grid()
        self.show = True
        
    def on_button(self):
        if self.show:
            self.master.config(cursor = 'none')
            self.show = False
        else:
            self.master.config(cursor = '')
            self.show = True
    
class App(tk.Tk):
    # app object inherits Tk
    def __init__(self):
        tk.Tk.__init__(self)
        
        self.title('Test cursor hide')
        self.geometry('300x50')

        Frame(self,width = 200).grid()
                   
# create and run an App object
App().mainloop()

mmkw43
Posts: 553
Joined: Tue Dec 24, 2013 6:18 pm

Re: Tkinter -- cursor invisible

Mon Jan 25, 2016 4:38 pm

great -- thank you so much.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: Tkinter -- cursor invisible

Mon Jan 25, 2016 4:54 pm

thank you for bringing my attention to this, im going to introduce the code I put up to my touch screen projects so the cursor is only visible on screen press, or maybe even not visible at all...

J.A.R.E.D.c.
Posts: 2
Joined: Tue Jul 10, 2018 12:20 pm

Re: Tkinter -- cursor invisible

Tue Jul 10, 2018 3:19 pm

KenT wrote:
Mon Jan 25, 2016 8:28 am
install unclutter
thank you!!!

Return to “Python”