Tkinter - Entry

INTRODUCTION

'Entry' widgets are used to get a 'single line' of text from a user, like a name or an email address.


import tkinter as tk   # Py3

root = tk.Tk()

label = tk.Label(root, text="Name", bg="red")
entry = tk.Entry(root, width=40)
button1 = tk.Button(root, text="Print", command=lambda: print(entry.get()))
button2 = tk.Button(root, text="Clear", command=lambda: entry.delete(0, tk.END))

label.grid()
entry.grid()
button1.grid()
button2.grid()

entry.focus()   # save a click
# The cursor will start in the entry field, so users don't have to click
# on it before starting to type.

entry.bind('<Return>', (lambda event: print(entry.get())))
# lambda is used to ignore the 'event' argument
root.bind('<Return>', (lambda event: print("message from root")))

root.mainloop()   # run the tkinter event loop

Selected keyword arguments in Entry()

master=root     # the first argument
fg="white"   # set the text color to white ('fg' or 'foreground')
bg="black"   # set the background color to black ('bg' or 'background')
width=50   # in text units (no height argument)
show='*'   # for passwords
state='disabled'   # set the widget readonly (default 'normal')
textvariable=strvar   # a variable linked to the entry

Operations

name = entry.get()   # get a string
entry.delete(pos)   # remove a char at pos
entry.delete(first, last)   # remove a range [first:last] ('last' not included)
entry.delete(first, tk.END)
entry.insert(pos, "text")   # insert text
entry.insert(0, "What is your name?")   # info for the user