1

I have a problem that annoys me. I am currently building a small app with a Tkinter GUI.

On the front page, I want some introductory text in either a text or a scrolledtext widget. The code examples I've come across uses keywords such as INSERT, CURRENT and END for indexation inside the widget.

I have literally copy pasted the below code into my editor, but it doesn't recognise INSERT (throws error: "NameError: name 'INSERT' is not defined"):

import tkinter as tk
from tkinter import scrolledtext

window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')

txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)

window.mainloop()

I can get the code to work if I change [INSERT] with [1.0], but it is very frustrating that I cannot get INSERT to work, as I've seen it in every example code I've come across

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Anders F.
  • 23
  • 1
  • 5

3 Answers3

5

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

import tkinter as tk
from tkinter import scrolledtext

window = 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() 
reboot
  • 108
  • 1
  • 6
2

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Great thanks. I actually found out about the raw strings by messing around long enough. I have an underlying frustration. I spend a lot of time searching for - and eventually finding stuff like this - I lack a proper and concise reference manual with clear and updated syntax. What do you more experienced guys use for syntax reference (e.g. for looking up, that INSERT is a tkinter.constant, but that you can use "insert" instead)? – Anders F. Mar 31 '20 at 18:23
  • 1
    @AndersF.: tkinter is just a wrapper around tcl/tk. This is described in the second paragraph in [the official python documentation for tkinter](https://docs.python.org/3/library/tk.html) The canonical, exhaustive documentation of every widget, and every option, can be found in the [tcl/tk man pages](https://www.tcl-lang.org/man/) – Bryan Oakley Mar 31 '20 at 18:36
1

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.

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • What's the difference between INSERT, CURRENT and END . I know what END is but I don't what's the difference between INSERT and CURRENT – K.E.S Mar 18 '22 at 04:59