How to take input from user in Tkinter GUI : Entry Widget


Entry Widget

Entry is widely used to take single line input from user or to display some data. in one line.

Syntax:

            Entry(master, options.........)

master : master is nothing but the window object (parent window for which Entry needs to be created )

options : options are used to shape the entry separated by comma's and to define action on click. Below is the list of options, can be passed while creating Entry.


  • bg : 
    • to give background colour
  • bd : 
    • to give border to the widget
  • fg : 
    • to give text a colour
  • cursor : 
    • allows to change the cursor icon when on Entry widget, only if you set as arrow, dot etc.
  • font : 
    • to set font for the text entered by user in Entry widget
  • exportselection : 
    • text entered, by default will got to clipboard, to avoid that we need to set it to 0
  • selectbackground : 
    • to give text a background colour
  • selectborderwidth : 
    • to give width around the selected txt
  • selectforeground : 
    • to dive colour to selected text
  • show : 
    • mainly used to hide user input, like in case of password
  • textvariable : 
    • to retrieve the data from Entry widget, must set to StringVar class
  • width : 
    • to give width to Entry widget as per no of character can be displayed in Entry widget 

Entry Class Built in Methods

  • delete(startindex, last=NONE) : 
    • this widget start deleting data from 'startindex' and will go up to end-1, if second argument 'last' is omitted, it will delete only current index data
  • get() : 
    • this method is used to collect user entered data from Entry Widget
  • icursor() : 
    • to set cursor just before a given index
  • index(index) : 
    • to shift the characters in entry widget so thet the passed index will be the left most
  • insert(index,m) : 
    • insert string m(second argument), at passed index(first argument)
  • select_adjust(index) : 
    • this method is to make sure that character at passed index is a part of selection text
  • select_clear() : 
    • to clear the selection, no action on no selection
  • select_from(index) : 
    • to select from a particular index, including the character at index
  • select_present() : 
    • to check if selection is present, if present returns True else returns False
  • select_range(start, end) : 
    • to select the text from start index to end-1 index, start index must be less than end index
  • start_to(index) : 
    • select all text from passed index+1 (excluding character at passed index)
  • xview(index) : 
    • to link Entry widget to horizontal scrollbar
  • xview_scroll(number, for_what) : 
    • to use horizontal scroll
      • number : to define scroll direction
        • positive number : left to right scroll
        • negative  number : right to left scroll
      • for what : to define scroll by 
        • UNITS : to scroll by width of characters
        • PAGES : to scroll by chunk the size of entry field.


there are few options which may not work on mac, but work finely on windows and linux

Example for the Entry widget, output screen shot is on top of this post


# program to create Entry widget

try:
from Tkinter import *
except:
from tkinter import *

mw = Tk()
mw.geometry(
'500x100+10+10')
mw.title(
"AETies : Automation Expert Technologies")

label = Label(mw
, text='Enter Name')
label.pack()

entry = Entry(mw )
entry.pack()

mw.mainloop()



 

 

Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India