简体   繁体   中英

Handling delete event in pygtk/glade

I have a GUI designed in glade , using python/gtk in the background.I want to handle the delete event and display a "Are you sure?"-message dialog.I have been trying to handle the delete and destroy events, but failing to do so.any light?

#!/usr/bin/python
import .... stuff




class App:
  def __init__(self):


    self.gladefile = 'test.glade'
    windowname = 'window'# This must match the window name in glade
    self.wTree = gtk.glade.XML(self.gladefile, windowname)# object for acessing widgets


    dic={
    # Also need to set project2's signal tab
       'on_window_delete_event':self.on_erro,
       'on_window_destroy_event':self.on_erro,
         }

    self.wTree.signal_autoconnect (dic)
    self.op=self.wTree.get_widget('window')
    self.op.show()

  def on_erro(self,widget,*args):

        print 'hello'






app = App()
gtk.main()

This code opens a simple window .On clicking on close button, it prints hello and exits.(I want the window to remain open)

You have to return True in order to stop propagation of the delete event in the callback on_erro as mentioned in the documentation for "delete-event" . In your current code, the callback is not returning any boolean value as required by the function, which I am guessing is returning False (Please check the signature for on_window_delete_event callback functions, the return type is boolean)
Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM