Python Tkinter Entry

python tutorials and learn python

Created with Sketch.

Python Tkinter Entry

The Entry widget is used to provde the single line text-box to the user to accept a value from the user. We can use the Entry widget to accept the text strings from the user. It can only be used for one line of text from the user. For multiple lines of text, we must use the text widget.

The syntax to use the Entry widget is given below.

Syntax

  1. w = Entry (parent, options)

A list of possible options is given below.

SNOptionDescription
1bgThe background color of the widget.
2bdThe border width of the widget in pixels.
3cursorThe mouse pointer will be changed to the cursor type set to the arrow, dot, etc.
4exportselectionThe text written inside the entry box will be automatically copied to the clipboard by default. We can set the exportselection to 0 to not copy this.
5fgIt represents the color of the text.
6fontIt represents the font type of the text.
7highlightbackgroundIt represents the color to display in the traversal highlight region when the widget does not have the input focus.
8highlightcolorIt represents the color to use for the traversal highlight rectangle that is drawn around the widget when it has the input focus.
9highlightthicknessIt 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.
10insertbackgroundIt represents the color to use as background in the area covered by the insertion cursor. This color will normally override either the normal background for the widget.
11insertborderwidthIt represents a non-negative value indicating the width of the 3-D border to draw around the insertion cursor. The value may have any of the forms acceptable to Tk_GetPixels.
12insertofftimeIt represents a non-negative integer value indicating the number of milliseconds the insertion cursor should remain “off” in each blink cycle. If this option is zero, then the cursor doesn’t blink: it is on all the time.
13insertontimeSpecifies a non-negative integer value indicating the number of milliseconds the insertion cursor should remain “on” in each blink cycle.
14insertwidthIt represents the value indicating the total width of the insertion cursor. The value may have any of the forms acceptable to Tk_GetPixels.
15justifyIt specifies how the text is organized if the text contains multiple lines.
16reliefIt specifies the type of the border. Its default value is FLAT.
17selectbackgroundThe background color of the selected text.
18selectborderwidthThe width of the border to display around the selected task.
19selectforegroundThe font color of the selected task.
20showIt is used to show the entry text of some other type instead of the string. For example, the password is typed using stars (*).
21textvariableIt is set to the instance of the StringVar to retrieve the text from the entry.
22widthThe width of the displayed text or image.
23xscrollcommandThe 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.

Example

  1. # !/usr/bin/python3
  2. from tkinter import *
  3. top = Tk()
  4. top.geometry(“400×250”)
  5. name = Label(top, text = “Name”).place(x = 30,y = 50)
  6. email = Label(top, text = “Email”).place(x = 30, y = 90)
  7. password = Label(top, text = “Password”).place(x = 30, y = 130)
  8. sbmitbtn = Button(top, text = “Submit”,activebackground = “pink”, activeforeground = “blue”).place(x = 30, y = 170)
  9. e1 = Entry(top).place(x = 80, y = 50)
  10. e2 = Entry(top).place(x = 80, y = 90)
  11. e3 = Entry(top).place(x = 95, y = 130)
  12. top.mainloop()

Output:

Python Tkinter Entry

Entry widget methods

Python provides various methods to configure the data written inside the widget. There are the following methods provided by the Entry widget.

SNMethodDescription
1delete(first, last = none)It is used to delete the specified characters inside the widget.
2get()It is used to get the text written inside the widget.
3icursor(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.
4index(index)It is used to place the cursor to the left of the character written at the specified index.
5insert(index,s)It is used to insert the specified string before the character placed at the specified index.
6select_adjust(index)It includes the selection of the character present at the specified index.
7select_clear()It clears the selection if some selection has been done.
8select_form(index)It sets the anchor index position to the character specified by the index.
9select_present()It returns true if some text in the Entry is selected otherwise returns false.
10select_range(start,end)It selects the characters to exist between the specified range.
11select_to(index)It selects all the characters from the beginning to the specified index.
12xview(index)It is used to link the entry widget to a horizontal scrollbar.
13xview_scroll(number,what)It is used to make the entry scrollable horizontally.

Example: A simple calculator

  1. import tkinter as tk
  2. from functools import partial
  3. def call_result(label_result, n1, n2):
  4.     num1 = (n1.get())
  5.     num2 = (n2.get())
  6.     result = int(num1)+int(num2)
  7.     label_result.config(text=“Result = %d” % result)
  8.     return
  9. root = tk.Tk()
  10. root.geometry(‘400×200+100+200’)
  11. root.title(‘Calculator’)
  12. number1 = tk.StringVar()
  13. number2 = tk.StringVar()
  14. labelNum1 = tk.Label(root, text=“A”).grid(row=1, column=0)
  15. labelNum2 = tk.Label(root, text=“B”).grid(row=2, column=0)
  16. labelResult = tk.Label(root)
  17. labelResult.grid(row=7, column=2)
  18. entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)
  19. entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)
  20. call_result = partial(call_result, labelResult, number1, number2)
  21. buttonCal = tk.Button(root, text=“Calculate”, command=call_result).grid(row=3, column=0)
  22. root.mainloop()

Output:

Python Tkinter Entry

 

Leave a Reply

Your email address will not be published. Required fields are marked *