Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Tkinter Entry Widget

Python Tkinter Entry Widget

Python GUI Programming Python GUI Programming

The Python Tkinter Entry widget is used to let the user enter a single line of text string.

Syntax

The syntax format is as follows:


w = Entry( master, option, ... )
No. Options & Description
1

bg

The background color of the entry field

2

bd

The size of the border, defaulting to 2 pixels

3

cursor

The shape of the cursor, such as arrow, circle, cross, plus, etc.

4

font

The text font

5

exportselection

By default, if you select text in the entry field, it is copied to the clipboard. To ignore this feature, set exportselection=0.

6

fg

Text color. The value is a color or a color code, such as 'red', '#ff0000'

7

highlightcolor

The highlight border color of the entry field, displayed when the field has focus

8

justify

When displaying multi-line text, set the alignment between different lines; options include LEFT, RIGHT, CENTER

9

relief

Border style, setting the 3D effect of the widget. Options are: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. The default is FLAT.

10

selectbackground

The background color of selected text

11

selectborderwidth

The background border width of selected text

12

selectforeground

The color of selected text

13

show

Specify a character to display the entry field's content as; the value can be any character. For example, for a password you can set the value to show="*"

14

state

The default is state=NORMAL. The field state can be read-only or writable, with values: normal/disabled

15

textvariable

The value of the entry field, which is a StringVar() object

16

width

The width of the entry field

17

xscrollcommand

Set the horizontal scrollbar, generally used when the width of the content entered in the field exceeds the displayed width of the field.

Method

The following table shows the commonly used methods of the Entry widget:

No. Method & Description
1

delete ( first, last=None )

Delete the value at a specified position in the entry field


   

text.delete(10) # delete the value at index 10 text.delete(10, 20) # delete values from index 10 up to (but not including) 20 text.delete(0, END) # delete all values | | 2 |

get()

Get the value of the entry field

| | 3 |

icursor ( index )

Move the cursor to the specified index position; this only works when the field has focus

| | 4 |

index ( index )

Return the specified index value

| | 5 |

insert ( index, s )

Insert a value into the entry field. index: the insertion position; s: the value to insert

| | 6 |

select_adjust ( index )

Select the value between the specified index and the current cursor position

| | 7 |

select_clear()

Clear the selection in the specified widget

| | 8 |

select_from ( index )

Set the cursor position, using the index value index

| | 9 |

select_present()

Return true if there is a selection, otherwise return false.

| | 10 |

select_range ( start, end )

Select the value at the specified index position. start (inclusive) is the beginning position, end (exclusive) is the ending position; start must be less than end

| | 11 |

select_to ( index )

Select the value between the specified index and the cursor

| | 12 |

xview ( index )

This method is useful when the entry field is linked to a horizontal scrollbar.

| | 13 |

xview_scroll ( number, what )

Used to scroll the entry field horizontally. The what parameter can be UNITS, to scroll by character width, or PAGES, to scroll by blocks of the field. For the number parameter, a positive value scrolls from left to right, and a negative value scrolls from right to left.

|

Example

In the example, clicking the button displays a message:

Example

#!/usr/bin/python

-- coding: UTF-8 --

from

Tkinter

import

top

=

Tk

(

)

L1

=

Label

(

top

,

text

=

"

Website name

"

)

L1

.

pack

(

side

=

LEFT

)

E1

=

Entry

(

top

,

bd

=

5

)

E1

.

pack

(

side

=

RIGHT

)

top

.

mainloop

(

)

The test output is as follows:

Python GUI Programming Python GUI Programming