Tkinter Text Tutorial
The construction method of Text is as follows.
Text(master, options, ...)
The first argument to the Text( ) method is the parent object, indicating which parent object this text area will be built inside. The following are other commonly used options parameters within the Text( ) method.
(1) bg or background
: the background color.
(2) borderwidth or bd
: border width, the default is 2 pixels.
(3) cursor
: the shape of the cursor when the mouse cursor is on the checkbox.
(4) exportselection
: if the selection operation is performed, the selected string will be automatically output to the clipboard, if you want to avoid this you can set exportselection=0.
(5) fg or foreground
: font color.
(6) font
: font.
(7) height
: height, the unit is the character height, the actual height will depend on the character height.
(8) highlightbackground
: the background color when the text box gets the focus.
(9) highlightcolor
: the color of the text box when it gets the focus.
(10) highlightthickness
: the thickness when get focus, default value is 1.
(11) insertbackground
: insert the color of the cursor, the default is black.
(12) insertborderwidth
: the 3D thickness around the inserted cursor, the default is 0.
(13) padx
: Text left/right box and the leftmost/rightmost spacing of the text.
(14) pady
: Text upper/lower box and the top/bottom of the text spacing.
(15) relief
: the default is relief=SUNKEN, which can control the text outside the box.
(16) selectbackground
: the background color of the selected string.
(17) selectborderwidth
: the thickness of the border when the string is selected, the default value is 1.
(18) selectforeground
: the foreground color of the selected string.
(19) state
: input state, the default is NORMAL, which means you can input, DISABLED is not editable.
(20) tab
: you can set how to position the insertion point when pressing Tab.
(21) width
: the width of Text, the unit is the character width.
(22) wrap
: can control the handling of a line of text is too long, the default is wrap=CHAR, when a line of text is too long, you can do line breaks from the characters; when wrap=WORD, only from the word to do line breaks.
(23) xscrollcommand
: use scrollbar in x-axis.
(24) yscrollcommand
: use scrollbar in y-axis.
Example#1
Create a Text text area with a height of 2 and a width of 30, then enter the text and observe the execution results.
from tkinter import *
root = Tk()
root.title("apidemos.com")
text = Text(root,height=2,width=30)
text.pack()
root.mainloop()
Execution results, below are the results without input:
Input 2 rows of data:
The result of entering 3 rows of data:
From the above figure can be found, if you enter more than two lines of text, will lead to the first line of data is hidden, if you enter more lines will cause more text is hidden, although you can use the cursor to move the way to see the first line of text again, but for people who do not understand the structure of the program, it is still easier to misunderstand the contents of the Text text area. Finally, it should be noted that the enlarged window does not enlarge the Text text area, see the figure below.
Of course, you can also reset the height and width parameters in the Text( ) method on line 7 to allow the Text text area to hold more data. But at least here the reader should be able to appreciate how to use the Text control to create a program for entering multiple lines of text.