[Tutor] Tkinter_Entry_tip words
Alan Gauld
alan.gauld at btinternet.com
Tue Dec 8 14:54:32 EST 2015
On 08/12/15 15:08, Yuehua HU wrote:
> I’ve tried to write 'user changing their mind and blanking the field’ case, but it seems not working…
> Did I use '<KeyPress-Delete>’ wrong?
Delete is the key that deletes the character to the right of cursor.
Did you maybe mean to use <'Keypress-BackSpace'>? Or even both of them?
> ...Why should we use a parameter in the function 'greyText(e)’ ?
Because the bind mechanism passes the event object as an argument
and expects the function to accept it even if its not used.
[Aside: This is annoying since the command attribute of widgets expects
a function with no arguments! To get round that set a default value of None:
def aCallBack(event=None):...
then you can use aCallBack() in either scenario.
]
HTH
> #######################
> import Tkinter
>
> def checkEnterStrLen(e):
> if len(var.get())==0:
> entry.config(fg= 'grey')
> entry.insert(0, 'help text')
>
> def greyText(e):
> entry.config(fg = 'grey')
>
> def startEntry(e):
> entry.delete(0,Tkinter.END)
> entry.config(fg = 'black')
> entry.unbind('<Key>')
>
> top = Tkinter.Tk()
>
> var = Tkinter.StringVar()
> entry = Tkinter.Entry(top, textvariable = var)
> entry.insert(0,'help text')
>
> entry.bind('<Button-1>', greyText)
> entry.bind('<KeyPress-Delete>',checkEnterStrLen)
> entry.bind('<Key>',startEntry)
>
> entry.pack()
>
> top.mainloop()
--
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