As a standard interface for Python GUI programming, tkinter has a very rich window control. This article introduces its Text component in detail.
Text component is a component that displays and processes multi-line text and has a very flexible application method.
The properties are divided into two categories, including STANDARD OPTIONS and WIDGET-SPECIFIC OPTIONS
. The basic description table is shown
STANDARD attribute value | Description |
background | Background color |
borderwidth | The border width of the text control. The default is 1-2 pixels. |
cursor | The cursor of the text control. The default is a character insertion cursor (usually an "I-beam" type cursor) |
exportselection | Whether to allow copying content to the clipboard |
font | Font settings and size |
foreground | Foreground color |
Define the highlight background color when the text control does not receive input focus. It's the bright edge of the text control. | |
The highlightbackground property is similar. It's just the border color of the text control when it gets input focus. | |
The border width of the text control when it gets input focus. Generally 1-2 pixels. |
Set the color of the text control insertion cursor | |
Insert cursor border width. If it is a non-zero value, the cursor will use a RAISED border. | |
insertofftimeinsertontime | These two properties control the insert cursor flashing effect. It is the time when the insertion cursor appears and disappears. The unit is milliseconds.|
Sets the width of the insertion cursor. | |
Padding in the x-axis direction | |
Padding in the y-axis direction Margin | |
Specifies the 3D border effect of the text control. The default is flat. The parameters that can be set are: flat, groove, raised, ridge, solid, sunken | |
Set the background color of the selected text | |
Set the border width of the selected area. |
setgrid | boolean type. When True, the window can be maximized and the entire Text control can be displayed. |
takefocus | Defines whether the Tab key can be used to move the input focus to the control. |
xscrollcommand yscrollcommand |
Associate the scroll bar with the text control and handle the scroll bar action. The scroll bars correspond to horizontal or vertical scroll bars respectively. |
WIDGET-SPECIFICWindow Properties | Description |
autoseparators | The space between words. The default value is 1 |
height | The height of the text control. The default is 24 lines. |
maxundo | Maximum number of Undo. The default is 0. |
spacing1 | Spacing before paragraph. |
spacing2 | Line spacing. |
spacing3 | Spacing after paragraph. |
state | Define the state of the text control. There are two states: NORMAL and DISABLED |
tabs | Define the movement distance when pressing the Tab key. |
undo | Enable undo/redo function. |
width | Define the width of the text control, the unit is the number of characters. |
wrap | Define how to wrap the content of the text control |
Methods of the Text basic class:
Description | |
Return a logical comparison result |
|
Calculate the number of related things between two indexes quantity. |
|
Delete the characters between INDEX1 and INDEX2 (not included) |
|
Return tuple (x, y, width, height, baseline) |
##edit(self, * args) |
This method controls the undo mechanism and the modified flag. |
get(self, index1, index2=None) |
Returns the value between index INDEX1 to INDEX2 (exclusive) |
image_create(self, index, cnf={}, **kw) |
Create an embedded image at the index INDEX position. |
Application example of Text component.
One is a simple basic application
from tkinter import *
class init_window():
def __init__(self):
self.tk = Tk ()
self.window_attritions()
def window_attritions(self):
self.tk.title('Text component learning')
self.init_data_text = Text(self.tk)
self.init_data_text.pack()
def show():
print('hello')
if __name__ == "__main__":
win = init_window()
win.tk. mainloop()
The second is to insert components
button = Button(win.init_data_text,text='Insert button in text',command=show)
win.init_data_text.window_create(INSERT,window=button)
The third is to insert pictures
win.init_data_text.image_create(END, image=PhotoImage(file='1.gif'))
It can be seen from the above introduction that the index value of the Text component has many applications. The following introduces the types of index values
insert(self, index, chars, *args) | |
mark_names(self) |
|
##replace(self, index1, index2, chars, *args) |
|
tag_unbind(self, tagName, sequence, funcid=None) |
|
tag_delete(self, *tagNames) |
Delete tag |
tag_remove( self, tagName, index1, index2=None) |
Remove specific tags between indexes |
window_create(self, index, cnf={}, **kw) |
Create a sub-window at the index position |
Index Type | Description |
line.column | Row/Column |
line.end | End of a line |
INSERT | Cursor position |
END | End of text |
user-defined marks | Custom marks |
user-defined tags("tag.first", "tag.last") | |
Window positioning |
The above is the detailed content of Text component of python3.6 tkinter. For more information, please follow other related articles on the PHP Chinese website!