In a piece of my code, my script generates a string to copy it in a scrolled text widget. if the size of the string is not so heavy, this process works without any kind of issues, but when the string is heavy, the script can't paste it into the scrolled text widget. when it happens, the script crashes, and an error message appears in the terminal: unable to alloc 27 bytes
(it's not an exception event).
before the crash, I got the bytes size of the string via sys.getsizeof
function, and it was 230031360 bytes (230 MB).
In these cases, the user can solve the issue by choosing to write the output message in a text file, but what about if the user tries to write a heavy string in the scrolled text widget anyway? In this specific case, I would very like to show a message box to advice the user to write the output in a text file, but how can I understand if the script can write the string in the scrolled text widget or not? what is the bytes limit of a string in Python?
UPDATE:
I wrote an example to show you where the issue occurs. the main window will crash in oround two minutes with an error message in the terminal, unable to alloc 28 bytes
:
from tkinter import *
from tkinter import ttk, scrolledtext
import ipaddress
def GiveMeHosts():
ls = []
for host in ipaddress.ip_network("10.0.0.0/8").hosts():
ls.append(str(host))
return ls
parent = Tk()
parent.geometry("400x350")
parent.title("The window will crash..")
MyWidget=scrolledtext.ScrolledText(parent, wrap=WORD, width=36, height=14, font=("Segoe UI", 9), state="normal")
MyWidget.pack()
parent.update()
# the string "hosts" is too long, and "MyWidget" can't manage it!
hosts = "\n".join(GiveMeHosts())
MyWidget.insert("1.0", hosts) # it makes the script to crash
parent.mainloop()