Frage

I found myself in the situation where I needed to close a window which I had subclassed from Gtk.Window. I was connecting the window.destroy event to close the window initially to Gtk.main_quit for testing, but when I loaded the module into my main program, I couldn't do it that way, of course, without closing the main program. When I wrote a function, I stupidly called the window.destroy() method which, stupid of me, resulted in a max recursion error. So, I decided to connect a 'delete-event' and then call the window.destroy() which still recursed because (as I learned) a destroy event calls delete-event. So, I connected the destroy event to a function and tried to call:

window.emit('delete-event')

which worked, but resulted in an error wanting another parameter. Researching, I learned it needed to be something like this from gtk2:

window.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE))

I can't find Gdk under Gtk like I can find gdk under gtk. I don't like this. I imported Gdk and Gdk.Event is found, but no equivalent to the gtk.gdk.DELETE variable. I did a:

l = dir(Gdk)
for i in l:
    if 'DELETE' in i:
        print(i)

and came up with no DELETE variable defined in Gdk.

How can we access Gdk from Gtk? If not, how do we translate these gtk.gdk... things when they don't exist under Gdk?

Ultimately, I just did:

del window

to do what I needed to do, but I still would like to know how to do the emit signal above in Gtk3 for learning purposes.

Thanks,

Narnie

War es hilfreich?

Lösung

There is no gtk.gdk in GTK 3, all of that functionality, or its equivalent, must be available using from gi.repository import Gdk. The previous gtk.gdk hierarchy was actually for convenience, but didn't make any sense because GDK is a completely independent package and is not part of GTK. The new organization makes more sense because the hierarchy and variable names are exactly the same as in the C, Javascript, etc. APIs.

In this case, gtk.gdk.DELETE is available as Gdk.EventType.DELETE, so you should be able to do

window.emit('delete-event', Gdk.Event(Gdk.EventType.DELETE))

However, you are not supposed to emit event signals yourself, so it would be better to do:

window.event(Gdk.Event(Gdk.EventType.DELETE))

Or even better, don't connect to the event at all in your module. Then you can use window.destroy() without a recursion error. Connect to the destroy signal in your main program, then you can simply call Gtk.main_quit.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top