How to loop through open Gtk.Windows?

34 Views Asked by At

So I am working with a tool written for an analysis program called Gwyddion which means I have to use Python 2.7. Switching to Python 3 is not an option.

We have a tool that will take an AFM scan, analyze it, and pop up a Gtk.Window (technically a subclass of a subclass of a Gtk.Window) with the results. I am trying to write something that takes a bunch of open AFM scans, runs the tool on 2 channels in each one, and takes screenshots of the windows that pop up. Unfortunately, I have run into an issue where for some reason the results don't actually display until after the loop running the tool on each channel finishes, so if I take screenshots as I go I just get white rectangles.

I have looked at the titles of EVERY SINGLE QUESTION TAGGED WITH BOTH GTK AND PYTHON (yes all 2k+ of them) and not found anyone else with this problem, so I think it's a problem with how the tool is written, which I can't change. Keeping that working properly is more important than automating taking the screenshots.

I thought maybe instead I can open all the windows, then loop through them, take a screenshot of each, and close them after taking the screenshot. But I can't find a command to simply list all the open Gtk.Windows so I can loop through them. I feel like I must be missing something obvious, as this seems like a basic thing to be able to do. So, how can I loop through all my open Gtk.Windows, and how do I know which order it's looping through them in so I can name the screenshots properly?

If anyone has ideas on how to make the windows draw as they are popped open rather than after the loop ends I would like that as well, which is why I gave the other background, but at this point I have pretty much accepted that I have to loop through them after they all open.

Possibly relevant: With the way Gwyddion interacts with gtk I don't actually have to run gtk.main(), so that's not anywhere in my code. I don't actually know why this is. I've only worked with gtk in the context of Gwyddion, so I don't really know what gtk.main() does.

Again, switching off of Python 2.7 or altering the tool that opens the windows are not options.

Edit: I don't know how I stared right at list_toplevels() and didn't realize that all my gtk.Windows are toplevel windows, but I'm still a little confused on what this means:

"If you want to iterate through the list and perform actions involving callbacks that might destroy the widgets, you must call g_list_foreach (result, (GFunc)g_object_ref, NULL) first, and then unref all the widgets afterwards."

I'm looking at the documentation for g_list_foreach and I can't figure out what (GFunc)g_object_ref refers to, or how to know what order it's going through them in. Would GFunc be my screenshot function? I also googled "gtk unref" and I don't know what "decreases the reference count" means or why it is necessary.

0

There are 0 best solutions below