1

I wanted to check if there is already an instance of Tk, to use my library that is built upon tkinter alongside with the original. The only indicator I have found is tkinter._default_root but at the same time I want to disable the support of default root. It appears to be a dilemma but following the tk.create to _tkinter_create_impl leads me to Tkpp_New C code with the form of:

    /* This is used to get the application class for Tk 4.1 and up */
    argv0 = (char*)PyMem_Malloc(strlen(className) + 1);
    if (!argv0) {
        PyErr_NoMemory();
        Py_DECREF(v);
        return NULL;
    }

But it relies on the className and the instance of Tk could be subclassed, so it seems unreliable to me. To be honest, I never wrote something in C, so I'm not entirely sure if this is really an option and worth to work it out.

So my question is: Is it possible to know if there is already an instance Tk in place ?

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • The C code you posted does not interact with Tkinter *at all*, so it's not capable of detecting anything. I think you left out something here. – jasonharper Oct 05 '22 at 13:40
  • @jasonharper Thanks for pointing this out, Updated. Based on my understanding. `tk.create` calls the C code `_tkinter_create_impl` which leads to `Tkapp_New` where they find the app with this C code. Am I wrong ? – Thingamabobs Oct 05 '22 at 13:48
  • @BryanOakley I'm working on a package for quite awhile now and I'm planning to share this lib in the future. Generally it would be possible to have that restriction, that if someone will use this lib they are forced to use my implementation of the `Tk`, or I would need to rewrite a significant amount of code and admit to my failure. I wonder if tkinter [send](https://stackoverflow.com/q/5573493/13629335) somehow can lead to such a result. – Thingamabobs Oct 05 '22 at 15:12
  • 1
    You can use the `gc` module to find all existing instances of `Tk`. Would that meet your needs? For example, see https://stackoverflow.com/a/328882/7432 – Bryan Oakley Oct 05 '22 at 15:21
  • 1
    Besides that it seems inefficient as is used in this link, I think this could be an good approach! – Thingamabobs Oct 05 '22 at 15:30
  • 1
    I presume you would only call it once, probably near the start of your program, so performance might not be much of an issue. – Bryan Oakley Oct 05 '22 at 22:03

1 Answers1

2

Thanks to Bryan Oakley for pointing me to this answer. After thinking it through, this seems to be the only valid choice. Considerations:

  1. The name of the app or the class can be changed.
  2. Calling tkinter methods would instantly create an instance of Tk
  3. Tracing the __init__ method would rely on the order of imports and time they are set*
  4. override the standard lib is unreliable and can lead to confusing results.

*Traceprofile or Metaclasses takes away options for users

gc.get_objects shouldn't really slow down the program, cause there shouldn't be this much objects in the beginning of a GUI script. Please feel free to answer another approach, if there is another valid approach.

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54