I have a program that gets some some text from a website
(link) then outputs it into a Text
widget.
But I want a way to check if the text exceeds the Text
widget so then I can call my clear_Text_Box()
function.
Is there any way to do this?
I searched up things like: tkinter how to know if my TEXT box is full, or tkinter Text box methods, but it gives me unrelated things such as how to create full screen window in tkinter and so on.
My Text
widget is (width = 30,height = 70)
.
My clear_Text_Box()
function:
def clear_Text_Box(Text_Box): # I have this function, so it clears the Text box if text
# exceeds the Text box.
Text_Box.config(state = 'normal')
Text_Box.delete('0.0','end')
Text_Box.config(state = 'disabled')
CodePudding user response:
Knowing if it's full can be a bit tricky at the very edge (ie: knowing if it's possible to squeeze in one more space or "i" character", but knowing whether or not some text has scrolled out of view is easy.
You can call the yview
and xview
methods of the widget each will return a tuple of two fractions: the amount above (or to the left) the viewable region and the amount below (or to the right). From the official documentation for the yview
method:
The first element gives the position of the first visible pixel of the first character (or image, etc) in the top line in the window, relative to the text as a whole (0.5 means it is halfway through the text, for example). The second element gives the position of the first pixel just after the last visible one in the bottom line of the window, relative to the text as a whole.
So, if you call yview
and xview
and they return (0.0, 1.0)
then everything in the widget is visible.
if Text_Box.xview() == (0.0, 1.0) and Text_Box.yview() == (0.0, 1.0):
# all text is visible
...