Tkinter Entry is used to take user input in Python GUI. A one-liner box is provided on the interface where the user can input text. It allows multiple line input to increases the height of the entry widget.
Syntax:
The parameter, master represents the parent window and the options are used to set various attribute as key-values separated by commas. The options are described below:
Options of Tkinker Entry:
bd: Size of border around the Entry box. By default is 2.
bg: Background color for the label.
command: This procedure is called to change the state of this widget.
font: Type of font used for the text.
justify: Specifies the text alignment within the widget. One of “left”, “center”, or “right”.
exportselection: To avoid exportation which is caused when you select text within an Entry widget, it automatically exported to the clipboard.
cursor: When the mouse is set over this widget, it changed to a cursor type given by you such as an arrow or dot.
xscrollcommand: Sometimes Entry widget is linked to a scrollbar when you expect that users will often enter more text than the onscreen size of the widget.
fg: Color used for the text.
textvariable: To retrieve the current text from the entry widget, set this option to an instance of the StringVar class.
highlightcolor: Show the text color when this widget is focused.
show: It controls the user input that appears in the entry box. If there is a password entry box, show = “*” is used.
selectborderwidth: Show the Width of border around the selected text. Default width is 1 pixel.
selectbackground: Background color to be used displaying selected text.
selectforeground: Foreground text color of selected text.
relief: Specifies the type of border. By default is Flat, other options include like RAISED
and SUNKEN
.
state: By default it’s NORMAL. DISABLED causes the Widget to gray out and go inactive. ACTIVE is the state of the Widget when the mouse is over it.
Methods of Tkinker Entry:
get(): This method returns the entry’s current text as a string.
insert ( index, s ): it inserts string before the character at the index box.
select_present(): This method returns true, if there is a selection, else returns false.
xview ( index ): This method is used to link the Entry widget to a horizontal scrollbar.
select_clear(): This method clears the selection.
icursor ( index ): This method set the insertion cursor just before the character at the index given.
delete ( first, last=None ): This method is used to delete characters from the widget, starting with the one at index first, up to but it does not include the character located in the last position. If the second argument is omitted, only the single character at position first is deleted.
index ( index ): This method shifts the contents of the entry so that the character at the given index is the leftmost visible character.
Example:
from tkinter import * appWindow = Tk() appWindow.title("Window Title-Tutorialsart.com") appWindow.geometry('350x200') frame1 = Frame(appWindow) frame1.pack() newentry1 = Entry(frame1, width=20) newentry1.insert(0, 'FULLNAME') newentry1.pack(padx=5, pady=5) newentry2 = Entry(frame1, width=15) newentry2.insert(0, 'ID NO.') newentry2.pack(padx=5, pady=5) appWindow.mainloop()
Comments are closed.