[Tutor] Tkinter_Entry_tip words
Yuehua HU
huyuehua1106 at sina.com
Tue Dec 8 10:08:44 EST 2015
Hi Alan,
Thank you for your answer, it is really helpful.
and it is not a homework :)
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?
and another question: Why should we use a parameter in the function 'greyText(e)’ ? Here the parameter is ‘e’.
#######################
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()
#######################
Thank you.
B.R.
Yuehua
> On Dec 7, 2015, at 22:23, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
> 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
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list