Tkinter Entry with Examples
The Tkinter Entry widget is used to allow the user to enter a line of text string.
The Entry widget is the most basic widget control for input in GUI programming. We can use it to enter a single line of string, and if the length of the input string is larger than the width of the text box, the text entered will be automatically hidden so that part of the content cannot be displayed. If you encounter this situation, you can use the arrow keys to move the mouse cursor to the area you can’t see. Note that the text box Entry is limited to a single line of text, if you want to handle multiple lines of text you need to use the Text in the Widget control.
Tkinter Entry Syntax
The format of Entry is as follows.
Entry(master, options, ...)
The first argument of the Entry( ) method is the parent object, which indicates the window in which this text box will be built.
The following are other commonly used options parameters within the Entry( ) method.
bg or background
: the background color.bd or borderwidth
: the default width of the border is 2 pixels.command
: this function will be executed automatically when user changes the content.cursor
: when the mouse cursor on the check box when the cursor shape.exportselection
: if the implementation of the selection, the selected string will be automatically output to the clipboard, if you want to avoid, you can set exportselection = 0.fg or foreground
: foreground color.font
: font.height
: height, the unit is the character height.highlightbackground
: the background color when the text box gets the focus.highlightcolor
: the color of the text box when it gets focus.justify
: the alignment of the last line when multiple lines of text are included.relief
: the default is relief=FLAT, which can control the text box.selectbackground
: the background color of the selected string.selectborderwidth
: the border width of the selected string, the preset is 1.selectfroeground
: the foreground color of the selected string.show
: show input characters, for example, show=’*’ means show asterisk, commonly used for entering password fields.state
: input state, the default is NORMAL means that you can enter, DISABLE means that you can not enter.textvariable
: text variable.width
: width, the unit is the character width.xscrollcommand
: use scroll bar in x-axis.
Tkinter Entry Examples
The following program example creates a label and text box inside the window to enter a name and address.
from tkinter import *
root = Tk()
root.title("apidemos_tkinter_entry") # Title of the window
nameL = Label(root, text="Name") # name Label
nameL.grid(row=0)
addressL = Label(root, text="Address") # address Label
addressL.grid(row=1)
nameE = Entry(root) # name entry
addressE = Entry(root) # address entry
nameE.grid(row=0, column=1) # location of name entry
addressE.grid(row=1, column=1) # location of address entry
root.mainloop()
Output:
The above line 8 set grid(row=0), in the absence of setting "column=x", the system will automatically set "column=0", the role of the 10th line is the same.