Home » Python Tkinter Entry

Python Tkinter Entry

by maxguy71

The Entry widget is used to create a  single line text-box to the user to accept a value from the user.

We can use the Entry widget to accept text strings from the user. It can only be used to get one line of text from the user. If you want to use multiple lines of text, use the text widget.

Syntax

Here is the syntax to create the entry widget −

w = Entry( master, option, ... )

Parameters

  • master − This represents the parent window.
  • options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Option Description
bg The background color of the widget.
bd The border width of the widget in pixels.
cursor The mouse pointer will be changed to the cursor type set to the arrow, dot, etc.
exportselection The text written inside the entry box will be automatically copied to the clipboard by default. This can bet set to 0 to not copy this.
fg This represents the color of the text.
font This represents the font type of the text.
highlightbackground This represents the color to display in the traversal highlight region when the widget does not have the input focus.
highlightcolor This represents the color to use for the traversal highlight rectangle that is drawn around the widget when it has the input focus.
highlightthickness This represents a non-negative value indicating the width of the highlight rectangle to draw around the outside of the widget when it has the input focus.
insertbackground This represents the color to use as background in the area covered by the insertion cursor. This color will override either the normal background for the widget.
insertborderwidth This represents a non-negative value indicating the width of the 3-D border to draw around the insertion cursor.
insertofftime This represents a non-negative integer value indicating the number of milliseconds the insertion cursor should remain “off” in each blink cycle. If this is zero, then the cursor is on all the time.
insertontime Specifies a non-negative integer value indicating the number of milliseconds the insertion cursor should remain “on” in each blink cycle.
insertwidth This represents the value indicating the total width of the insertion cursor.
justify This specifies how the text is organized if the text contains multiple lines.
relief This specifies the type of the border. The default value is FLAT.
selectbackground The background color to use displaying selected text.
selectborderwidth The width of the border to use around selected text. The default is one pixel.
selectforeground The font color of the selected task.
show This is used to show the entry text of some other type instead of the string
textvariable This is set to the instance of the StringVar to retrieve the text from the entry.
width The width of the displayed text or image.
xscrollcommand The entry widget can be linked to the horizontal scrollbar if we want the user to enter more text then the actual width of the widget.

 

Methods

These are the following methods provided by the Entry widget.

Method Description
delete(first, last = none) This is used to delete the specified characters inside the widget.
get() This is used to get the text written inside the widget.
icursor(index) It is used to change the insertion cursor position. We can specify the index of the character before which, the cursor to be placed.
index(index) This is used to place the cursor to the left of the character written at the specified index.
insert(index,s) This is used to insert the specified string before the character placed at the specified index.
select_adjust(index) This includes the selection of the character present at the specified index.
select_clear() This clears the selection if some selection has been done.
select_form(index) This sets the anchor index position to the character specified by the index.
select_present() This returns true if some text in the Entry is selected otherwise returns false.
select_range(start,end) This selects the characters to exist between the specified range.
select_to(index) This selects all the characters from the beginning to the specified index.
xview(index) This is used to link the entry widget to a horizontal scrollbar.
xview_scroll(number,what) This is used to make the entry scrollable horizontally.

 

Code Examples

 

from tkinter import *
 
def getdata():
    print(entry1.get())
    print(entry2.get())
 
root = Tk()
root.geometry("200x150")
 
frame = Frame(root)
frame.pack()
 
entry1 = Entry(frame, width = 20)
entry1.insert(0,'Username')
entry1.pack(padx = 5, pady = 5)
 
entry2 = Entry(frame, width = 15)
entry2.insert(0,'Password')
entry2.pack(padx = 5, pady = 5)
 
Button = Button(frame, text = "Submit", command = getdata)
Button.pack(padx = 5, pady = 5)
 
root.mainloop()

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More