0

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()

thumbnail

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
  • The question you posted has updates from within the past few years so it seems like it's still relevant. It seems like the X window system has an internal limit to the number of resource identifiers it can create, and you're hitting that limit. If all else fails, I'd think the best solution is to either render the thumbnails into big chunks instead of separate images, or only generate thumbnails that are meant to be currently visible. I'm no UI designer so those might not be great suggestions, keep in mind. – Random Davis Feb 10 '23 at 16:41
  • 1
    Please create a minimally reproducible example that demonstrates this problem. – Сергей Кох Feb 10 '23 at 17:33
  • See [mre]. And @Сергей Кох, you can use the magic link ```[mre]```. Anyway I suspect a bug in your code because the memory was sufficient. – relent95 Feb 11 '23 at 02:33
  • Are you using `tkinter.update` in your code? Also have a look at this [answer](https://stackoverflow.com/a/75112734/13629335) don't know if it could be related to your environment. – Thingamabobs Feb 11 '23 at 07:16
  • @Thingamabobs My code did not use `tkinter.update` command. Also, I made sure the syntax `.update` is not used but it is always `.update_somethingelse`. Could be the issue. Same as what @Randomdavis suggested. – Sun Bear Feb 11 '23 at 07:23
  • @Thingamabobs You commented `In one case I used generator and in a different approach I was using a memory-mapped file object.` in the other answer, can you provide me links to such examples so I can understand how you did it? – Sun Bear Feb 11 '23 at 07:43
  • Unfortunately I can't, I still didn't do a github account and my old projects directory is a mess. I needed to search over an hour through them for your last question. But the principle is explained by Donal in his community wiki. – Thingamabobs Feb 11 '23 at 08:58
  • @Thingamabobs Thanks. Suffice it for now. The above comments have been really helpful; pointed out the weakness in the design of my GUI. I will figure this out with time. :) – Sun Bear Feb 11 '23 at 15:01

0 Answers0