0

"python tkinter entry" How to get a string before a input cursor in entrybox?

entry.get(0,tk.INSERT)

TypeError: get() takes 1 positional argument but 3 were given

ebinem
  • 21
  • 3

1 Answers1

1

Try this:

import tkinter as tk

def get():
    print(entry.get()[:entry.index("insert")])

root = tk.Tk()

entry = tk.Entry(root)
entry.pack()

button = tk.Button(root, text="Click me", command=get)
button.pack()

root.mainloop()

It gets the contents of the entry then uses string slicing to get all of the characters before entry.index("insert"), which is the insert cursor's position. For more info please look at this and this.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31