[Tutor] Tkinter Entry field text

Wayne Werner waynejwerner at gmail.com
Sat Sep 10 16:16:20 CEST 2011


On Thu, Sep 8, 2011 at 9:31 PM, brandon w <thisisonlyatest at gmx.com> wrote:

> How do you display text in a Entry field and have it disappear when a
> person clicks in it?
> <snip some code> To get text into this box the person must first delete
> what is already in there.
>
> Python 2.6.6
>

 Think about the process. You're already mostly there: you're displaying
data already, and you know what you want to do.

You'll want to take a look at binding events, and if you Google for "tkinter
events" (http://www.google.com/search?q=tkinter+events) then your first
result takes you here:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

To point you in the right direction, you want to take a look at the
<FocusIn> event. If you're not familiar with what a callback is, you should
also read this page: http://effbot.org/zone/tkinter-callbacks.htm

If you still have problems after that, show us what else you've tried and
we'll be happy to give you more pointers in the right direction.



Of course, while this answers your questions, I would be remiss if I didn't
suggest a few more things about your program in general.

> label1 = Label(root, text="Enter you password: ")

label1 isn't a terribly descriptive name. That's fine if you don't intend to
actually do anything with the label. However, you can make this even more
explicit by chaining the commands together. Since Label() returns a label,
you can add a dot to the end and treat it just like you would the variable:

Label(root, text="Enter you password: ").grid(sticky=W, row=0, column=0)

That will create your label and stick it in the grid in one step, and makes
it clear to anyone reading your code (including yourself down the road!)
that you don't care to do anything with that label.

Next:

> enter_data1 = Entry(root, bg = "pale green")

enter_data1 also suffers from the same naming problem. It doesn't describe
what the variable is or does very well, aside from entering data. You could
change it to something like "password_entry" - which tells anyone reading
your program that the variable should contain something that lets you do
some password entry. Just naming it password would also be better than
enter_data1.

One other issue that you should consider - with the options you have set,
anyone could see what you typed in as your password. If you're just using
this as a testing program to play around with, that's probably OK, but
what's even better is to change what's shown in the box. You can do this by
setting the "show" option, either in the constructor or somewhere later:

from Tkinter import *

root = Tk()
entry = Entry(root)
entry.pack()
entry.insert(0, "My Cool Password")
entry.config(show="*")
root.mainloop()

The nice thing about the config() method is that it allows you to change
config attributes later. You can combine this knowledge with some of what I
mentioned earlier to show 'password' until they navigate to the field, and
then just show asterisks.

For bonus points, if they leave the password field without typing a
password, can you make it show 'password' again?

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110910/d41c801b/attachment.html>


More information about the Tutor mailing list