Control whether a Tkinter. Text is empty

Asked

Viewed 429 times

0

I’m trying to create a simple notepad, but I have a little problem: when I try to see if the text of an object Text is or is not empty:

if text_area.get(0.0, tkinter.END) != "":
    print("Text não vazio") # é sempre executado

When I try to use the function len to control the length of the text, returns 1.

What is this default character? My solution would be to control the length of the text with 1 (or perhaps better, if the length is greater than 1), and if it returns True, it means that it is "empty".

By the way, why Python, or better the bookstore tkinter provides a text end indicator, ie tkinter.END, and not at first? Why do I have to put 0.0 (and not tkinter.START)?

(In comparison for example to the Java language, and in particular to its Swing bookstore, the Tkinter bookstore is really not intuitive, even frank sincerely)

1 answer

0


Try the following:

if text_area.get(1.0, tkinter.END) != "":
    print("Text não vazio")

1.0 Means line 1, spine 0, lines are indexed from 1 and columns are indexed in 0.

At the following link(Text widget indices) describes some ways to work with widgets using the Tkinter.

  • Undoubtedly the Tkinter is an excellent module, but there are other very good ones like Gtk, Qt. Here has something on. About the len I still don’t know how to help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.