I encountered the following error message while running a python script that I am working on. It occurred while tkinter was loading ~1800 thumbnail images, each being 200x200 pixels large, into individual ttk.Checkkbuttons
. It did not finish this process and the program crashed with this error message.
X Error of failed request: BadAlloc (insufficient resources for operation)
Major opcode of failed request: 53 (X_CreatePixmap)
Serial number of failed request: 140089
Current serial number in output stream: 140097
The system that I ran this python tkinter script has a large RAM capacity. Before the program crashed, the RAM utilisation was only around 10%.
I don't understand this error message. Can you please explain what is causing this error?, Is this error caused by a limitation of the tkinter package or of the system's hardware (e.g. RAM, GPU, ... etc)? Anyway to overcome this?
I found a similar question but it was posted 9 years ago. I do not know if it is still relevant. Appreciate your assistance.
Update:
Following what was tried by the similar question, I added:
Option "VideoRam" "65536"
Option "CacheLines" "1980"
into the Section "Device"
segment of /etc/X11/xorg.conf , i.e it is now:
Section "Device"
Identifier "Device0"
Driver "nvidia"
VendorName "NVIDIA Corporation"
Option "VideoRam" "65536"
Option "CacheLines" "1980"
EndSection
The error changed to:
X Error of failed request: BadAlloc (insufficient resources for operation)
Major opcode of failed request: 53 (X_CreatePixmap)
Serial number of failed request: 128544
Current serial number in output stream: 128552
The Current serial number in output stream: 140097
dropped to 128552.
The explanation of XCreatePixmap is given here. It states that BadAlloc occurs when the server failed to allocate the requested source or server memory.
I tried to create a test code to simulate the error. Although it failed to simulate the X BadAlloc error, it provides a simplified view of the scenario when the error occurred in my code. Nonetheless, I did learn that the max grid rows or columns allowed by tkinter is < 10000 because it will return _tkinter.TclError: row out of bounds
. So if my original code is hitting the error when the quantity value used is 1800, is not related to _tkinter.TclError: row out of bounds
. I wonder if my error is related to one of the thumbnails supplied to my original code.
Test code:
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
class App(ttk.Frame):
def __init__(self, parent, file, quantity):
super().__init__(parent)
self.parent = parent
self.quantity = quantity
self.image = ImageTk.PhotoImage(Image.open(file))
self.checkbuttons = []
self.checkvalues = []
self.create_widgets()
def create_widgets(self):
for i in range(self.quantity):
self.checkvalues.append(tk.BooleanVar())
self.checkbuttons.append(
ttk.Checkbutton(self,
image=self.image,
variable= self.checkvalues[-1])
)
self.checkbuttons[-1].grid(row=i, column=0)
if __name__ == "__main__":
root = tk.Tk()
app = App(root, 'thumbnail.jpg', 9000)
app.grid(row=0, column=0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
root.mainloop()