YouTip LogoYouTip

Python Tkinter Entry

# Python2.x Python Tkinter Text Box (Entry) [![Image 4: Python GUI Programming](#) Python GUI Programming](#) Python Tkinter Text Box (Entry) is used to allow users to input a single line of text string. * If you need to input multiple lines of text, you can use the _Text_ widget. * If you need to display one or more lines of text and do not allow the user to modify it, you can use the _Label_ widget. ### Syntax The syntax format is as follows: w = Entry( master, option, ... ) * master: The parent container of the button. * options: Optional parameters, i.e., the settable properties of the button. These options can be set in the form key = value and separated by commas. | No. | Options & Description | | --- | --- | | 1 | **bg** Background color of the input box | | 2 | **bd** Size of the border, default is 2 pixels | | 3 | **cursor** Shape of the cursor, such as arrow, circle, cross, plus, etc. | | 4 | **font** Font of the text | | 5 | **exportselection** By default, if you select text in the input box, it will be copied to the clipboard. To ignore this function, you can set exportselection=0. | | 6 | **fg** Text color. The value can be a color name or a color code, e.g., 'red', '#ff0000' | | 7 | **highlightcolor** Highlight border color of the text box, displayed when the text box gains focus | | 8 | **justify** When displaying multiple lines of text, set the alignment between different lines. Options include LEFT, RIGHT, CENTER | | 9 | **relief** Border style, sets the 3D effect of the control. Options are: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. Default is FLAT. | | 10 | **selectbackground** Background color of selected text | | 11 | **selectborderwidth** Border width of the background of selected text | | 12 | **selectforeground** Color of selected text | | 13 | **show** Specifies that the content of the text box is displayed as characters. The value can be any character. For example, for a password, you can set the value to show="*" | | 14 | **state** Default is state=NORMAL. Text box state, divided into read-only and writable. Values are: normal/disabled | | 15 | **textvariable** Value of the text box, which is a StringVar() object | | 16 | **width** Width of the text box | | 17 | **xscrollcommand** Sets the horizontal scrollbar, generally used when the width of the user-input text box content is greater than the displayed width of the text box. | ### Methods The following table lists the common methods of the text box widget: | No. | Method & Description | | --- | --- | | 1 | **delete ( first, last=None )** Deletes the value at the specified position in the text box. text.delete(10) # Deletes the value at index 10. text.delete(10, 20) # Deletes values from index 10 up to (but not including) 20. text.delete(0, END) # Deletes all values. | | 2 | **get()** Gets the value of the text box. | | 3 | **icursor ( index )** Moves the cursor to the specified index position. This only works when the text box has focus. | | 4 | **index ( index )** Returns the specified index value. | | 5 | **insert ( index, s )** Inserts a value into the text box. index: insertion position, s: value to insert. | | 6 | **select_adjust ( index )** Selects the value at the specified index and before the cursor position. | | 7 | **select_clear()** Clears the selection in the specified control. | | 8 | **select_from ( index )** Sets the cursor position using the index value. | | 9 | **select_present()** Returns true if there is a selection, otherwise returns false. | | 10 | **select_range ( start, end )** Selects the value at the specified index position. start (inclusive) is the start position, end (exclusive) is the end position. start must be less than end. | | 11 | **select_to ( index )** Selects the value between the specified index and the cursor. | | 12 | **xview ( index )** This method is useful when the text box is linked to a horizontal scrollbar. | | 13 | **xview_scroll ( number, what )** Used to scroll the text box horizontally. The what parameter can be UNITS, scrolling by character width, or PAGES, scrolling by text box component blocks. The number parameter: a positive number scrolls from left to right, a negative number scrolls from right to left. | ### Example In this example, clicking the button will display 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: !(#) [![Image 6: Python GUI Programming](#) Python GUI Programming](#)
← Prop Cssstyle LengthPhp Password_Hash β†’