'what is bad text index in tkinter?
I am a tkinter beginner and I would like to ask what is this
Traceback (most recent call last):
File "C:\Users\HP\Desktop\2.py", line 23, in <module>
box.insert( "win2",x)
File "C:\Users\HP\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 3272, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: bad text index "win2"
although i change 'win2' to others but the error still have they said the word is bad text index
this is my code:
from tkinter import*
from tkinter.ttk import*
from tkinter import ttk
from tkinter import messagebox
from tkinter import scrolledtext
x= "ww"
a = 0
qty = 10
win2 = Tk()
win2.title ("abc ")
win2.geometry ("400x400")
win2.maxsize (400,400)
win2.configure(bg= 'white')
box = scrolledtext.ScrolledText (win2, wrap = WORD, width = 29, height = 13, font = ("courier", 10))
box.place (x = 10, y= 10)
while a != qty :
a= a + 1
box.insert( "win2",x)
box.config (state= 'disabled')
after i removed
box.insert( "win2",x)
the code can run without error
Solution 1:[1]
The meaning of your error traceback is as follows:self.tk.call((self._w, 'insert', index, chars) + args) means that the insert method called on your widget (self._w), takes an index, and chars as arguments. (it is TclTk syntax for python text.insert(ndx, chars))
tkinter.TclError: bad text index "win2" means that the argument "win2" provided is not a valid index for this method call.
tk.Text indices are strings of the form line.column
from Dr Shipman's documents:
An index is a general method of specifying a position in the content of a text widget. An index is a string with one of these forms:
'line.column'The position just before the given column (counting from zero) on the given line (counting from one). Examples: '1.0' is the position of the beginning of the text; '2.3' is the position before the fourth character of the second line.
Solution 2:[2]
the first argument in insert() is meant to be the index you want to insert text. like "1.0" for line 1, character 0.
EDIT:
http://effbot.org/tkinterbook/text.htm#Tkinter.Text.insert-method
See Inserting Initial Text on https://www.pythontutorial.net/tkinter/tkinter-text/
Solution 3:[3]
The insert method of the tkinter text widget takes text index of where to insert the text as argument, which cannot be a random string like "win2", thus it throws the error bad text index "win2".
The general function syntax for insert is -:
text_widget_object.insert(text_index, text_to_insert, tags = None) # tags is an optional argument.
Further info on the insert method can be found here.
.insert(index, text, tags=None)
Inserts the given text at the given index.
If you omit the tags argument, the newly inserted text will be tagged with any tags that apply to the characters both before and after the insertion point.
If you want to apply one or more tags to the text you are inserting, provide as a third argument a tuple of tag strings. Any tags that apply to existing characters around the insertion point are ignored. Note: The third argument must be a tuple. If you supply a list argument, Tkinter will silently fail to apply the tags. If you supply a string, each character will be treated as a tag.
Also, from what it seems like you are trying to loop around and insert text on 10 lines of the widget if so, the code for the same is as below -:
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import scrolledtext
x= "ww"
a = 0
qty = 10
win2 = Tk()
win2.title ("abc ")
win2.geometry ("400x400")
win2.maxsize (400,400)
win2.configure(bg= 'white')
box = scrolledtext.ScrolledText(win2, wrap = WORD, width = 29, height = 13, font = ("courier", 10))
box.place (x = 10, y= 10)
while a != (qty + 1) : # For 10 lines (qty + 1), as line 10 is represented by 10.0 unlike index notation.
a += 1
index_str = str(a) + '.0' # generating the index notation
box.insert(index_str, x + '\n') # inserting the string
#box.config(state= 'disabled') # NOTE: This disables, your scrolled text widget and you wont be able to insert more to it.
NOTE: If you are trying to insert into multiple lines using the loop, note that the last line of the loop body i.e. box.config(state = 'disabled') will disable the scrolled text widget, and any further insertions would not show effect on it, to see the effects, it is suggested to comment it/not use it at all, depending on your use case.
Two common use cases are described below further customization can be done, using the knowledge of the text indices, and the syntax as provided above.
If you have to insert the text at the end of the text widget, the last index of the text widget is set as a tkinter constant namely tkinter.END. Now we can insert the text at the end like so -:
box.insert(tkinter.END, x)
Further if the text has to be inserted to the very start of the text widget, then you can use the line index "1.0" like so -:
box.insert("1.0", x)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 |
