Detect user logout / shutdown in Python / GTK under Linux - SIGTERM/HUP not received

前端 未结 2 1557
遥遥无期
遥遥无期 2021-01-18 00:04

OK this is presumably a hard one, I\'ve got an pyGTK application that has random crashes due to X Window errors that I can\'t catch/control.

So I created a wrapper t

2条回答
  •  误落风尘
    2021-01-18 00:36

    You forgot to close gtk's event loop.

    This code exits with code 0 when you close the window:

    import gtk
    
    class Test(gtk.Window):
        def destroy_event(self, widget, data=None):
            gtk.main_quit()
    
        def __init__(self):
            gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
            self.connect("destroy", self.destroy_event)
            self.show()
    
    f = Test()
    gtk.main()
    

    EDIT: Here's the code to catch the SIGTERM signal:

    import signal
    
    def handler(signum, frame):
        print 'Signal handler called with signal', signum
        print 'Finalizing main loop'
        gtk.main_quit()
    
    signal.signal(signal.SIGTERM, handler)
    

    The rest of the code is exactly as above, no changes. It works here when I send SIGTERM to the python process: gtk main loop ends and program exits with exit code 0.

提交回复
热议问题