[Tutor] Tkinter_Entry_tip words

Alan Gauld alan.gauld at btinternet.com
Mon Dec 7 09:23:04 EST 2015


On 07/12/15 13:12, Yuehua HU wrote:

> Function description:
> User input strings in Entry(Tkinter) widget, there are tip words displayed in this Entry widget, 
> when the Entry widget is selected, the tip words are faded,
> when user begin to entering words into this Entry, the tip words are disappeared.
> 
> Does anybody know the method to implement it with Entry and Label widget? Or any other method in python?

I'm hoping this is not a homework...

Try something like this for Python 2:

######################
import Tkinter as tk

top = tk.Tk()
e = tk.Entry(top)
e.pack()

def greyText(ev):
   e.config(foreground='grey')

def startEntry(ev):
   e.delete(0,tk.END)
   e.config(foreground='black')
   e.unbind('<Key>')

e.insert(tk.END,"Help text")

e.bind('<ButtonRelease-1>', greyText)
e.bind('<Key>', startEntry)

top.mainloop()
##########################

You'll need some tweaks to cater for the user changing
their mind and just blanking the field. In that case you
probably need to reinstate the hint and the key binding.
I leave that as an exercise...

By coincidence I was doing something very similar
to this yesterday! :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list