[Tutor] Tkinter Entry field text
brandon w
thisisonlyatest at gmx.com
Sun Sep 11 08:15:26 CEST 2011
On 09/10/2011 10:16 AM, Wayne Werner wrote:
> On Thu, Sep 8, 2011 at 9:31 PM, brandon w <thisisonlyatest at gmx.com
> <mailto: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
Wayne,
Your advice was extremely helpful. I pointed me into the right
direction. I got the code to work after some time. I know how to make it
show the "password" again. I would have to do this:
from Tkinter import *
root = Tk()
root.title("Show me your Password")
root.geometry("375x100+600+250")
root.grid()
def callback(event):
passwd_entry.delete(0, END) # It would come back because there is
no .insert() function.
return
[....snip...]
#=========================
Here is the final working code:
#!/usr/bin/python
from Tkinter import *
root = Tk()
root.title("Show me your Password")
root.geometry("375x100+600+250")
root.grid()
def callback(event):
field0 = varText1.get()
passwd_entry.delete(0, END)
passwd_entry.insert(0, field0)
return
def showPasswd():
field1 = varText.get()
passwd_display.delete(0, END)
passwd_display.insert(0, field1)
return
Label(root, text="Type your password in this box: ").grid(sticky=W,
row=0, column=0)
Label(root, text="Your password is:").grid(sticky=W, row=2, column=0)
varText = StringVar()
varText.set("Enter password")
passwd_entry = Entry(root, textvariable=varText, bg = "pale green")
passwd_entry.bind("<Enter>", callback)
passwd_entry.config(show="*")
passwd_entry.grid(row=0, column=1)
varText1 = StringVar(None)
passwd_display = Entry(root, textvariable=varText1, bg = "pale green")
passwd_display.grid(row=2, column=1)
Button(root, text="Show Password", width=10,
command=showPasswd).grid(sticky=W, row=1, column=0)
root.mainloop()
Thanks a lot for your help!!!!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110911/35bffd27/attachment.html>
More information about the Tutor
mailing list