The Entry widget is a standard Tkinter widget used to enter or display a single line of text.
When to use the Entry Widget
The entry widget is used to enter text strings. This widget allows the user to enter one line of text, in a single font.
To enter multiple lines of text, use the Text widget.
Patterns #
To add entry text to the widget, use the insert method. To replace the current text, you can call delete before you insert the new text.
e = Entry(master)
e.pack()
e.delete(0, END)
e.insert(0, "a default value")To fetch the current entry text, use the get method:
s = e.get()
You can also bind the entry widget to a StringVar instance, and set or get the entry text via that variable:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()This example creates an Entry widget, and a Button that prints the current contents:
from Tkinter import * master = Tk() e = Entry(master) e.pack() e.focus_set() def callback(): print e.get() b = Button(master, text="get", width=10, command=callback) b.pack() mainloop()
e = Entry(master, width=50) e.pack() text = e.get()
def makeentry(parent, caption, width=None, **options): Label(parent, text=caption).pack(side=LEFT) entry = Entry(parent, **options) if width: entry.config(width=width) entry.pack(side=LEFT) return entry user = makeentry(parent, "User name:", 10) password = makeentry(parent, "Password:", 10, show="*")
content = StringVar() entry = Entry(parent, text=caption, textvariable=content) text = content.get() content.set(text)
FIXME: More patterns to be added.
In newer versions, the Entry widget supports custom events. Document them, and add examples showing how to bind them.
Add ValidateEntry subclass as an example?
Concepts
Indexes
The Entry widget allows you to specify character positions in a number of ways:
- Numerical indexes
- ANCHOR
- END
- INSERT
- Mouse coordinates (“@x”)
Numerical indexes work just like Python list indexes. The characters in the string are numbered from 0 and upwards. You specify ranges just like you slice lists in Python: for example, (0, 5) corresponds to the first five characters in the entry widget.
ANCHOR (or the string “anchor”) corresponds to the start of the selection, if any. You can use the select_from method to change this from the program.
END (or “end”) corresponds to the position just after the last character in the entry widget. The range (0, END) corresponds to all characters in the widget.
INSERT (or “insert”) corresponds to the current position of the text cursor. You can use the icursor method to change this from the program.
Finally, you can use the mouse position for the index, using the following syntax:
"@%d" % xwhere x is given in pixels relative to the left edge of the entry widget.
Reference #
- Entry(master=None, **options) (class) [#]
-
A text entry field.
- master
- Parent widget.
- **options
- Widget options. See the description of the config method for a list of available options.
- config(**options) [#]
-
Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.
- **options
- Widget options.
- background=
- Widget background. The default is system specific. (the option database name is background, the class is Background)
- bg=
- Same as background. <