[PYTHON] Addictive points when displaying logs with Tk.text

at first

I found an unexpected trap when I was investigating a memory leak problem at work, so I will post it. I think it will be a bit random, but please forgive me.

environment

OS:Windows10

Anaconda PowerShell Prompt


(base) PS C:\Users\95315> python -V
Python 3.6.10 :: Anaconda, Inc.
(base) PS C:\Users\95315> conda -V
conda 4.9.2
(base) PS C:\Users\95315>conda list
…
tk                        8.6.8                hfa6e2cd_0
…

What i wanted to do

https://tkdocs.com/tutorial/text.html I'd like you to refer to the Example: Logging Window section on the above page, but I made a program that pulls the contents of the logger and displays it in Tk.text. The point is the code below.

python


from tkinter import *
from tkinter import ttk

root = Tk()
log = Text(root, state='disabled', width=80, height=24, wrap='none')
log.grid()

def writeToLog(msg):
    numlines = int(log.index('end - 1 line').split('.')[0])
    log['state'] = 'normal'
    if numlines==24:
        log.delete(1.0, 2.0)
    if log.index('end-1c')!='1.0':
        log.insert('end', '\n')
    log.insert('end', msg)
    log['state'] = 'disabled'

What happened

I deleted it from my head when the number of lines displayed by the logger exceeded a certain number, but even so, the memory consumption increased and eventually it fell due to lack of memory for each PC, which is a headache problem. Was out ...

What was wrong

When I searched for various things, the following reference described the cause of the slightly old one. https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/text-undo-stack.html

The Text widget has a built-in mechanism that allows you to implement undo and redo operations that can cancel or >reinstate changes to the text within the widget.

In short, Tk.text has an optional undo/redo stack. It's convenient. Yeah. I wish I hadn't turned it on and behaved like an unlimited number of stacks.

So how did you fix it

All you have to do is disable the stack. In the code I mentioned earlier, I'll fix it.

python


from tkinter import *
from tkinter import ttk

root = Tk()
log = Text(root, state='disabled', width=80, height=24, wrap='none',undo=False)
log.grid()

def writeToLog(msg):
    numlines = int(log.index('end - 1 line').split('.')[0])
    log['state'] = 'normal'
    if numlines==24:
        log.delete(1.0, 2.0)
    if log.index('end-1c')!='1.0':
        log.insert('end', '\n')
    log.insert('end', msg)
    log['state'] = 'disabled'

Just add undo = False. ~~ How much time do I have for this party ... ~~ Well, it was a rather nasty story, so let's write it in the article. ~~ Otherwise, drinking will not go down. ~~ That's why I wrote an article.

At the end

Even TKDocs, which is linked from the official, has this kind of thing, so what can I trust anymore ... For the time being, I thought it was important to explore various things, not just one source. So Tkinter doesn't mean shit or anything like that. I mean, there are various GUIs in Python, and I think it's okay to use Tkinter, which is provided as standard. If you are worried that some of these troubles are buried, you may consider using a good library. After that, I think it's okay to write in Qiita's article that there was such a trouble with collective intelligence. When I have a hard trouble, I feel a little refreshed if I think that I have made a story to write on Qiita. ~~ So it's a scary story from here, but there is still one more memory leak factor left and I have to find and crush it too ... Well, I don't think there are many people who get caught, so I hope you can think that there is such a thing as a reading material. Thank you for reading this far.

Recommended Posts

Addictive points when displaying logs with Tk.text
Points to note when performing logistic regression with Statsmodels
Addictive points when downloading files using boto on AWS Lambda
What I was worried about when displaying images with matplotlib