What's the most efficient way to get a Tkinter Text widget's total display lines after the insert?

前端 未结 2 1016
野趣味
野趣味 2021-01-22 04:57

Is there a more efficient way to get the total number of display lines (not just visible ones) that are left in a tkinter Text widget after the text in

相关标签:
2条回答
  • 2021-01-22 05:13

    If you only care about the time to scroll through the document, and if the data doesn't change as you're scrolling (ie: new lines aren't being added) you can simply calculate how many pixels the document takes up (you only have to do this once), and where you currently are in the document. That will tell you how many pixels are left. From that you should be able to calculate an approximation of how much time it will take.

    You can get the total number of pixels used in the text widget with myTextWidget.count("1.0", "end", "ypixels". You can then get the y coordinate of the current line with myTextWidget.dlineinfo("insert"). From those two numbers you can compute a percentage of how far the insertion point is from the end of the widget.

    0 讨论(0)
  • 2021-01-22 05:13

    Okay, I found an answer (and yes, this is purposefully an answer; it's not part of my question).

    The following code will much more efficiently give the number of display lines left after the text insert:

    def get_display_lines(start="insert", end="end-1c")
        try:
            count=myTextWidget.count(start, end, "displaylines")[0]
        except TypeError:
            count=0
        return count
    

    Although this is much more efficient, it is still not totally efficient and will have trouble with large files (if you're using the arrow keys a lot during autoscroll). The count() method returns a tuple; hence, the zero index. Because this does not always contain a number (like when the insert is at the end of the document), that is why I use a try/except block (although you could do this lots of other ways; however you please).

    Because this is not efficient for large files, you might just get the current lines left once, save them, and increment/decrement this variable when updating the status (granted, that will require editing a bunch of functions, but it is more efficient). You'll need to update the total during edits, though (fortunately, that is less common while autoscrolling).

    Because messing around with a lot of other functions is a lot of overhead, and increases the complexity of editing the code, I suggest just only having it update on the autoscroll intervals, rather than when the user manually moves the cursor. Also, to increase efficiency, don't have it calculate the lines left (let alone the time left from that) in the statusbar update method, but only calculate it in a saved variable from the method called by the timer, and just print the variable in the statusbar.

    0 讨论(0)
提交回复
热议问题