[Tutor] Tkinter_Entry_tip words

Yuehua HU huyuehua1106 at sina.com
Wed Dec 9 06:03:54 EST 2015


Hi Alan,

it works using ‘BackSpace’ and ‘Delete’!
There is no ‘BackSpace’ key on Mac, so I didn’t come up with this key before, thanks for your advise and answer :)

B.R.
Yuehua


> On Dec 9, 2015, at 03:54, Alan Gauld <alan.gauld at btinternet.com> wrote:
> 
> 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
> 
> 
> _______________________________________________
> 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