Python Tkinter Text Box Widget is the advanced version of the Entry widget and covers all the drawbacks of Entry widgets in Python Tkinter. In this tutorial, we will learn everything about Text Widget. Also, we will cover these topics.
- Python Tkinter Text Box Not Editable
- Python Tkinter Text Box Size
- Python Tkinter Text Box Scrollbar
- Python Tkinter Text Box Clear
- Python Tkinter Text Box Get Value
- Python Tkinter Text Box Word Wrap
- Python Tkinter Text Box Variable
- Python Tkinter Text Box Set Text
- Python Tkinter Text Box Example
- Python Tkinter Text Bold
If you are new to Tkinter, check out Python TKinter Programming.
Python Tkinter Text Box Not Editable
- Text box widget in Python Tkinter provides a state option using which the Text box can be disabled. Once disabled, the user won’t be able to edit the the content of Text box in Python Tkinter.
- State of the Text box can be either disabled or normal and the syntax to use state option is:
# set the Text widget to Not Editable mode
text_box.config(state='disabled')
# set the Text widget to Editable mode
text_box.config(state='normal')
Full Code:
Here is the full code for the implementation of Text box to Not Editable.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#84BF04')
message ='''
Dear Reader,
Thank you for giving your
Love and Support to PythonGuides.
PythonGuides is now available on
YouTube with the same name.
Thanks & Regards,
Team PythonGuides '''
text_box = Text(
ws,
height=12,
width=40
)
text_box.pack(expand=True)
text_box.insert('end', message)
text_box.config(state='disabled')
ws.mainloop()
Output:
Here is the output of the above code, in this output user, won’t be able to make any changes to the provided text.
Read: Python Tkinter Entry – How to use
Python Tkinter Text Box Size
Text Box Size in Python Tkinter can be adjusted by changing the value of height and width of the Text box widget.
- Height is the number of rows in the Text box widget.
- Width determines the number of columns in the Text box widget.
- In the below code snippet we have provided height as 12 and width as 40.
text_box = Text(
ws,
height=12,
width=40
)
text_box.pack()
Full code of the Program:
Here is the full code for the Program to implement Text box size in Python Tkinter. Height & width makes determines the size of Text box in Python Tkinter.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#A67449')
message ='''
Dear Reader,
Don't let this situation
blind your future. We at
PythonGuides write tutorials
with real life examples to
make you understand the concept
in best possible way.
Thanks & Regards,
Team PythonGuides '''
text_box = Text(
ws,
height=13,
width=40
)
text_box.pack(expand=True)
text_box.insert('end', message)
ws.mainloop()
Output of Text Box Size in Python Tkinter:
In this output, you can count the number of lines to find the height of the program and width can be found by counting a number of words including spaces.
Read: Python Tkinter Button – How to use
Python Tkinter Text Box Scrollbar
Data inside a Python Tkinter Text box could be more than the size of the screen. In that case, scrollbars help the users to navigate horizontally and vertically on the screen.
- In this section, we will learn how to implement scrollbars on the Text Box in Python Tkinter.
- The best practice to implement scrollbars is to put the Python Tkinter Text box widget and Scrollbar widget inside one frame and adjust their positions to the right and left.
- Here is an example of implementing Scrollbars in the Python textbox widget.
Here is the program for scrollbar using Text box in Python Tkinter
from tkinter import *
def extract_data():
print(text_box.get('1.0', 'end'))
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#84BF04')
message ='''
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
'''
frame = Frame(ws)
text_box = Text(
frame,
height=13,
width=20,
wrap='word'
)
text_box.insert('end', message)
text_box.pack(side=LEFT,expand=True)
sb = Scrollbar(frame)
sb.pack(side=RIGHT, fill=BOTH)
text_box.config(yscrollcommand=sb.set)
sb.config(command=text_box.yview)
frame.pack(expand=True)
ws.mainloop()
Here is the output of the program. In this output vertical scrollbars are added to the Text box widget in Python Tkinter.
Read: Python Tkinter Progress bar – How to use
Python Tkinter Text Box Clear
Text box widget in Python Tkinter provides a delete() method using which we can clear the content of the text box widget.
- delete() method accepts two arguments to clear the content of the Text box widget:
- starting point: accepts float value; starts from 1.0; it deletes the provided row.
- ending point: accepts float value; type ‘end’ to delete the entire data from starting point. to delete specific rows simply mention the float value of the row.
- Here is the sample code that you can try to understand the delete method of the Text Box widget in Python Tkinter. simply replace the below code with the full code at line 4.
text_box.delete(1.0, 5.0) # delete lines 1, 2, 3, 4
text_box.delete(5.0, 'end') # delete all lines except first four lines
Full code for the Python Tkinter Text Box Clear Program.
Here is the full source code of program in which we have cleared the Text box widget using delete method in Python Tkinter.
from tkinter import *
def clear_textbox():
text_box.delete(1.0, 'end')
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#84BF04')
message ='''
Dear Reader,
Don't let this situation
blind your future. We at
PythonGuides write tutorials
with real life examples to
make you understand the concept
in best possible way.
Thanks & Regards,
Team PythonGuides '''
text_box = Text(
ws,
height=13,
width=40
)
text_box.pack(expand=True)
text_box.insert('end', message)
Button(
ws,
text='Clear',
width=15,
height=2,
command=clear_textbox
).pack(expand=True)
ws.mainloop()
Output for the Python Tkinter Text Box Clear Program:
In this output, we have demonstrated how to clear content of Text box in Python Tkinter. The interface shows a Text box and a button. You can write any text inside the Text box and then if you will click on the button all the text will be deleted.
Read: Python Tkinter Canvas Tutorial
Python Tkinter Text Box Get Value
The text box in Python Tkinter offers a method Get which allows fetching all the information stored in the Text box. Get method requires Index as an argument that specifies the range of fetching the information.
Syntax:
In this syntax, 1.0 is the starting position and the end keyword refers to the end of the document. that means all the data will be fetched from the Text box.
text_box.get(1.0, 'end')
Source code of get() method in Text box.
Here is the Program to demonstrate the get() method in Python Tkinter. In this program we will be displaying the information on the terminal that was inserted in the Text box.
from tkinter import *
from tkinter import messagebox
def extract_data():
print(text_box.get('1.0', 'end'))
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#84BF04')
message ='''
You are invited to a Birthday Party
venue: Az Resort
Timing: 7 pm, wednesday
Please visit with family.
Regards,
James
'''
text_box = Text(
ws,
height=13,
width=40,
wrap='word'
)
text_box.pack(expand=True)
text_box.insert('end', message)
Button(
ws,
text='Change Text',
command=extract_data
).pack(expand=True)
ws.mainloop()
Output:
In this output, when user clicked on the change text button then, the text starts appearing the terminal. So this is only possible using get() method.
Read: Python Tkinter Progress bar – How to use
Python Tkinter Text Box Word Wrap
The wrap is used to shift the word to the next line if the margin is reached to improve the readability. Python Tkinter Text box widget provides an option wrap using which we can wrap up the characters or words of the sentences inside Text box widget in Python Tkinter.
- A sentence in the Text box widget can be wrapped by words or characters.
wrap='char'
, is used to wrap the sentence by character. Here the word breaks into sentences to cover the available space just before reaching the margin.wrap='word'
, is used to wrap the sentence by the word. Here the words will jump to the next line if they didn’t fit the space before reaching margin.- This code snippet is picked from the full source code below. Here we have set the wrap to ‘word’. The full program of code is available below this code snippet.
text_box = Text(
ws,
height=13,
width=40,
wrap='word'
)
Full code to perform word wrap on Text Box in Python Tkinter
In this program, we have wrapped the text by words. That means if the words will shift to new line if the margin is reached.
from tkinter import *
def clear_textbox():
text_box.delete('1.0', 'end')
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#84BF04')
message ='''
Dear Reader,
Thank you for giving your Love and Support to PythonGuides. PythonGuides is now available on YouTube with the same name.
Thanks & Regards,
Team PythonGuides '''
text_box = Text(
ws,
height=13,
width=40,
wrap='word'
)
text_box.pack(expand=True)
text_box.insert('end', message)
ws.mainloop()
Output of Text box word wrap in Python Tkinter
In this output, two pictures are displayed with the caption Word wrap and character wrap. In the left picture if there is no space left for the word then entire word shifts to next line whereas in the right picture ‘char wrap’ is applied that means if there is a space for a single character then that character will sit there while other characters of word will shift to new line.
Python Tkinter Text Box Variable
- Text Box widget in Python Tkinter does not support variable, you won’t be able to use variable and the possible reason is data is added using insert method in Text box widget.
- Variable is used with other widgets like Entry, check buttons, radio buttons, etc.
- Please note that here variable doesn’t mean the name or characters that store the value of object, class, or any other component. That variable is universal and used with each widget and program. like in our case text_box is the variable assigned to the Text Box widget.
- Variable here is referred to the one that keeps a record of changes and it can be StringVar, IntVar, BooleanVar, etc.
unknown option "-variable"
this error means variables can’t be used with the Text box widget in Python Tkinter.
Python Tkinter Text Box Set Text
- The set text option is to set the text on the widget but it is not applicable on the Text box widget.
- In the Text box, we insert the data line by line similarly we delete the data line by line. The developer has to pass the argument about the start and end position of new data.
- But while using set text we don’t need to pass any argument related to the position of data. This could be the possible reason that why the Set text option or method is not available for the Text box.
- If set text is used in Text Box than following error will appear.
AttributeError: 'Text' object has no attribute 'set'
Python Tkinter Text Box Example
- Text boxes are widely used in Desktop applications, Games, software like text editors, etc.
- In this tutorial, we have shared multiple examples of Text box widgets in Python Tkinter that can be referred to for major or minor projects.
Python Tkinter Text Bold
In this section, we will learn how to change text from normal to bold in Text box widget. Also, we use the above mentioned program but with the change in text type to Bold.
- Using font, we can style the look and feel of the application. The font offers three things (font name, font size, and font-weight.
- The font name can Arial, times new roman, etc there is n number of fonts and you can choose any of them but it must be supported by your system.
- The font Size determines the predefined value of height and width. Changes to the number will increase or decrease the font size.
- The font-weight determines whether the font will look bold, italic, and underline.
from tkinter import *
def clear_textbox():
text_box.delete('1.0', 'end')
ws = Tk()
ws.title('PythonGuides')
ws.geometry('500x400')
ws.config(bg='#84BF04')
message ='''
Dear Reader,
Thank you for giving your
Love and Support to PythonGuides.
PythonGuides is now available on
YouTube with the same name.
Thanks & Regards,
Team PythonGuides '''
text_box = Text(
ws,
height=13,
width=40,
font= ('Arial', 16, 'bold')
)
text_box.pack(expand=True)
text_box.insert('end', message)
ws.mainloop()
Output of the above code:
In this output, you can see that the text is appearing larger and it is bold. It can done by altering the values in font option.
You may like the following Python Tkinter articles:
- Python Tkinter Stopwatch
- Python Tkinter Listbox – How to Use
- Python tkinter messagebox
- Python Tkinter Frame
- How to make a calculator in Python
- Python Tkinter Menu bar – How to Use
In this tutorial, we have learned about Python Tkinter Text Box and how to use the Text box widget in Python Tkinter. Also, we have covered below mentioned topics.
- Python Tkinter Text Box Not Editable
- Python Tkinter Text Box Size
- Python Tkinter Text Box Scrollbar
- Python Tkinter Text Box Clear
- Python Tkinter Text Box Get Value
- Python Tkinter Text Box Word Wrap
- Python Tkinter Text Box Variable
- Python Tkinter Text Box Set Text
- Python Tkinter Text Box Example
- Python Tkinter Text Bold
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.