Header Ads

Header ADS

Text widget of tkinter

   Multiline input using Text widget in tkinter    

The Text widget of tkinter provides us the facility to take multiline input from the user and it is very useful when you are creating GUI application like Notepad .

so in this blog you are going to learn  about Text widget and how to set and fetch data from it .

Syntax of Text()

       
        text = Text(master,attributes)
 


 In the given syntax text is the instance of Text widget ,we can also create a Text box without using any instance but for setting and fetching data we must need a instance of Text widget .


Text( ) widget full code ..


       

from tkinter import *
root = Tk()
text = Text(root)
text.pack()
root.mainloop()
      

The GUI with textbox is looks like : 


As you can see we can write in our text box .

Attributes of Text widget ..

1
bg
The default background color of the text widget.
2
bd
The width of the border around the text widget. Default is 2 pixels.
3
cursor
The cursor that will appear when the mouse is over the text widget.
4
exportselection
Normally, text selected within a text widget is exported to be the selection in the window manager. Set exportselection=0 if you don't want that behavior.
5
font
The default font for text inserted into the widget.
6
fg
The color used for text (and bitmaps) within the widget. You can change the color for tagged regions; this option is just the default.
7
height
The height of the widget in lines (not pixels!), measured according to the current font size.
8
highlightbackground
The color of the focus highlight when the text widget does not have focus.
9
highlightcolor
The color of the focus highlight when the text widget has the focus.
10
highlightthickness
The thickness of the focus highlight. Default is 1. Set highlightthickness=0 to suppress display of the focus highlight.
11
insertbackground
The color of the insertion cursor. Default is black.
12
insertborderwidth
Size of the 3-D border around the insertion cursor. Default is 0.
13
insertofftime
The number of milliseconds the insertion cursor is off during its blink cycle. Set this option to zero to suppress blinking. Default is 300.
14
insertontime
The number of milliseconds the insertion cursor is on during its blink cycle. Default is 600.
15  
insertwidth
Width of the insertion cursor (its height is determined by the tallest item in its line). Default is 2 pixels.
16
padx
The size of the internal padding added to the left and right of the text area. Default is one pixel.
17
pady
The size of the internal padding added above and below the text area. Default is one pixel.
18
relief
The 3-D appearance of the text widget. Default is relief=SUNKEN.
19
selectbackground
The background color to use displaying selected text.
20
selectborderwidth
The width of the border to use around selected text.
21
spacing1
This option specifies how much extra vertical space is put above each line of text. If a line wraps, this space is added only before the first line it occupies on the display. Default is 0.
22
spacing2
This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0.
23
spacing3
This option specifies how much extra vertical space is added below each line of text. If a line wraps, this space is added only after the last line it occupies on the display. Default is 0.
24
state
Normally, text widgets respond to keyboard and mouse events; set state=NORMAL to get this behavior. If you set state=DISABLED, the text widget will not respond, and you won't be able to modify its contents programmatically either.
25
tabs
This option controls how tab characters position text.
26
width
The width of the widget in characters (not pixels!), measured according to the current font size.
27
wrap
This option controls the display of lines that are too wide. Set wrap=WORD and it will break the line after the last word that will fit. With the default behavior, wrap=CHAR, any line that gets too long will be broken at any character.
28
xscrollcommand
To make the text widget horizontally scrollable, set this option to the set() method of the horizontal scrollbar.
29
yscrollcommand
To make the text widget vertically scrollable, set this option to the set() method of the vertical scrollbar.

Methods Used with Text widget ..

1
delete(startindex [,endindex])
This method deletes a specific character or a range of text.
2
get(startindex [,endindex])
This method returns a specific character or a range of text.
3
index(index)
Returns the absolute value of an index based on the given index.
4
insert(index [,string]...)
This method inserts strings at the specified index location.
5
see(index)
This method returns true if the text located at the index position is visible.
       
from tkinter import *

def function():
    print(text.get(0.0,END))
root = Tk()
text = Text(root,bd=5,relief=GROOVE,fg='blue',bg='pink',height=10,width=50)
text.pack()

btn = Button(root,text='fetch Textbox text',command=function)
btn.pack()
root.mainloop()    
 


The following code creates a Text box and  a button also which is used for fetching text box data ..
you can see i pass ( 0.0,END) in the get method , the 0.0 means start from row 0 and column 0 and stops at END of text ..

The GUI will looks like ..
And when i press the button the value written inside the Text widget is printed ..




This blog is contributed by Yogesh singh ..
you can check my previous blog on tkinter and projects .
you can also follow my Instagram page for amazing tkinter and pyqt5 projects .
.
if you like this blog please comment your feedback ..

1 comment:

Powered by Blogger.