This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811343
''Tkinter text (1 )'''
'''1. Create the first text '''
From tkinter import *
Root = TK ()
T = text (Root)
T. Pack ()
Root. mainloop ()
# Root contains a text control. You can enter text in the control. You can use Ctrl + C/V to add the content (text) on the clipboard to the text ), do not accept Ctrl + z operation
'''2. add text to text '''
# Add text content using the insert method
From tkinter import *
Root = TK ()
T = text (Root)
# Add text 0123456789 to the first row and the first column
T. insert (1.0, '123 ')
# Add the text abcdefghij to the first column of the First row
T. insert (1.0, 'abcdefghj ')
T. Pack ()
Root. mainloop ()
# The first parameter of insert is the index, and the second parameter is the added content.
'''3. add content using the line. COL index '''
# Use indexes to add text content
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text (Root)
# Add text 0123456789 to the first row and the first column
T. insert (1.0, '123 ')
T. insert ('2. end ','')
# Add the text abcdefghij to the first column of the First row
T. insert (2.5, 'abcdefghj ')
T. Pack ()
Root. mainloop ()
# When indexes is used, if its value exceeds the buffer value of text, the program will not throw an exception and it will use the approximate value.
'''Mark is a type of symbol used to represent the position in text '''
'''4. Use the built-in Mark Control to add a location '''
# Demonstrate the built-in usage of Mark: insert/current/end/sel_first/sel_last
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text (Root)
# Add 10 lines of text to text
For I in range (1, 10 ):
T. insert (1.0, '123 ')
# Define the callback functions of each button. These functions use the built-in MARK: insert/current/end/sel_first/sel_last
Def inserttext ():
T. insert (insert, 'jcodeer ')
Def currenttext ():
T. insert (current, 'jcodeer ')
Def endtext ():
T. insert (end, 'jcodeer ')
Def selfirsttext ():
T. insert (sel_first, 'jcodeer ')
Def sellasttext ():
T. insert (sel_last, 'jcodeer ')
# Insert
Button (root,
TEXT = 'insert jcodeer at insert ',
Command = inserttext
). Pack (fill = X)
# Current
Button (root,
TEXT = 'insert jcodeer at current ',
Command = inserttext
). Pack (fill = X)
# End
Button (root,
TEXT = 'insert jcodeer at end ',
Command = endtext
). Pack (fill = X)
# Sel_first
Button (root,
TEXT = 'insert jcodeer at sel_first ',
Command = selfirsttext
). Pack (fill = X)
# Sel_last
Button (root,
TEXT = 'insert jcodeer at sel_last ',
Command = sellasttext
). Pack (fill = X)
T. Pack ()
Root. mainloop ()
# Several built-in marks:
# Insert: insert point of the cursor
# Current: The character location corresponding to the current location of the mouse
# End: The last character of the text buffer.
# Sel_first: the first character of the selected text field. If no area is selected, an exception is thrown.
# Sel_last: The last character of the selected text field. If no area is selected, an exception is thrown.
'''5. Use an expression to enhance the mark '''
# Expressions can define any indexes as follows:
'''
+ Count chars: Forward count characters
-Count chars: Move back count characters
+ Count lines: Forward count rows
-Count lines: move back to count rows
Linestart: Start of moving to the row
Linesend: The End of moving to the row
Wordstart: Move to the start of a word
Wordend: Move to the end of the word
'''
# Demonstrate how to use modifier expressions and how to use them with currently available indexes
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text ()
# Add text 0123456789 to the first row and the first column
For I in range (1, 10 ):
T. insert (1.0, '123 ')
A = 'test _ mark'
Def forwardchars ():
# Directly connect strings
# T. mark_set (A, current + '+ 5 chars ')
T. mark_set (A, current + '+ 5C ')
Def backwardchars ():
# T. mark_set (A, current + '-5 chars ')
T. mark_set (A, current + '-5C ')
Def forwardlines ():
# T. mark_set (A, current + '+ 5 lines)
T. mark_set (A, current + '+ 5l ')
Def backwardlines ():
# T. mark_set (A, current + '-5 lines)
T. mark_set (A, current + '-5l ')
Def linestart ():
# Note that the space before linestart cannot be omitted
T. mark_set (A, current + 'linestart ')
Def lineend ():
# Note that the space before lineend cannot be omitted
T. mark_set (A, current + 'lineend ')
Def wordstart ():
# Move to the start of the current word.
T. mark_set (A, current + 'wordstart ')
Def wordend ():
# Move to the end of the current word
T. mark_set (A, current + 'wordend ')
# MARK: test_mark; default value: Current
T. mark_set (A, current)
Button (root, text = 'forward 5 chars', command = forwardchars). Pack (fill = X)
Button (root, text = 'backward 5 chars', command = backwardchars). Pack (fill = X)
Button (root, text = 'forward 5 lines', command = forwardlines). Pack (fill = X)
Button (root, text = 'backward 5 lines', command = backwardlines). Pack (fill = X)
Button (root, text = 'line start', command = linestart). Pack (fill = X)
Button (root, text = 'line end', command = lineend). Pack (fill = X)
Button (root, text = 'word start', command = lineend). Pack (fill = X)
Button (root, text = 'word end', command = lineend). Pack (fill = X)
# Test the differences between the three positions. The current state shows the position of the current cursor. The mark indicates the position of the mark, and the insert operation does not change at 1.0.
Def inserttext ():
T. insert (insert, 'insert ')
Def currenttext ():
T. insert (current, 'current ')
Def marktext ():
T. insert (a, 'mark ')
Button (root, text = 'insert jcodeer.cublog.cn ', command = inserttext). Pack (fill = X)
Button (root, text = 'current jcodeer.cublog.cn ', command = currenttext). Pack (fill = X)
Button (root, text = 'Mark jcodeer.cublog.cn ', command = marktext). Pack (fill = X)
T. Pack ()
Root. mainloop ()
Text (1) In tkinter tutorials