Home > front end >  python tkinter text: the widget always contain a newline character
python tkinter text: the widget always contain a newline character

Time:08-25

I'm using a tkinter.Text widget, and I noticed that it cannot be empty (not contain text). I tried:

import tkinter as tk

root = tk.Tk()

textbox = tk.Text(root)

print("ini")
#textbox.delete("1.0", tk.END)
#textbox.insert(tk.INSERT, "ey")
#print(textbox.get("1.0", tk.END))
print(repr(textbox.get("1.0", tk.END)))
print("end")

root.mainloop()

And I get:

ini
'\n'
end

Is there any way to remove that character, textbox.delete("1.0", tk.END) doesn't work. Of course, I can always do textbox.get("1.0", tk.END)[:-1]

CodePudding user response:

Solved using:

textbox.get("1.0", tk.END "-1c")

Thanks to this question and the answer of @xxmbabanexx

  • Related