Skip to content Skip to sidebar Skip to footer

Python3/tkinter Text "displaylines" Behavior Issue

I am trying to count the actual lines used in a text box when it is pre-filled with data, and again when the form is submitted. With the code below, it incorrectly shows the number

Solution 1:

displaylines can't be calculated if the data isn't displayed, since the value depends on various factors such as the screen resolution, the actual font (which might be different than the requested font), the size of the widget once pack, place, or grid and done it's calculations, and so on. These can't be determined until the window is actually mapped to the screen.

To illustrate the point, add top.update() immediately before calling countit() and you'll see that the value is correctly printed the first time.

A better fix would be to not call countit until the window has been mapped to the screen. For example, you can add this line of code after creating the text widget:

DE.bind("<Map>", lambda event: countit())

The above will call countit() immediately after the widget has been mapped to the screen.

Post a Comment for "Python3/tkinter Text "displaylines" Behavior Issue"