Tkinter text widget - Why does INSERT not work as text index? Tkinter text widget - Why does INSERT not work as text index? tkinter tkinter

Tkinter text widget - Why does INSERT not work as text index?


Use tk.INSERT instead of only INSERT. Full code is shown.

import tkinter as tkfrom tkinter import scrolledtextwindow = tk.Tk()window.title("test of scrolledtext and INSERT method")window.geometry('350x200')txt = scrolledtext.ScrolledText(window,width=40,height=10)txt.insert(tk.INSERT,'You text goes here')txt.grid(column=0,row=0)window.mainloop() 


You don't need to use the tkinter constants. I personally think it's better to use the raw strings "insert", "end", etc. They are more flexible.

However, the reason the constants don't work for you is that you're not directly importing them. The way you're importing tkinter, you need to use tk.INSERT, etc.


INSERT could not be used directly.

You can use it in the past just because you used this in the past:

from tkinter import * # this is not a good practice

INSERT,CURRENT and END are in tkinter.constants.Now in your code,you even didn't import them.

If you want to use them,you can use

from tkinter.constants import * # not recommended...txt.insert(INSERT,'You text goes here')

Or

from tkinter import constants...txt.insert(constants.INSERT,'You text goes here') # recommend

If didn't want to import them,you can also use:

txt.insert("insert",'You text goes here')

Edit:I found in the source code of tkinter,it had import them,reboot's answer is also OK.