[pygtk] Noob Advice: Best way to kill (self) from within __init__?

dave mcodes davemcodes at gmail.com
Thu May 7 06:55:52 WST 2009


Hi folks,

First off, I'm a noob to OOP, Python and PyGTK, so please forgive a little
ignorance here.

I've built a simple image viewer class and I'm trying to figure out how to
gracefully destroy an instance of it from within __init__ depending on
weather or not a given condition exists.

Logic is basically thus:

class viewer:
    if no files:
         post a dialog box
         upon "OK" button click, bail out and kill this instance
    else:
         finish creating viewer objects
         start an event loop and do something upon button press event.


The trouble I have is that I can block the main event loop with run(), but I
can't figure out how to bail out gracefully and destroy this instance. I'm
not sure if del(self) is appropriate. See snip below.

Further, I'm also not sure if it's appropriate to nest gtk_main() here since
it prevents me from handling any top level events until I exit this
instance. I read somewhere that it's bad form to reference the parent from
within a child class instance (and therefore, I should not try to register a
callback from my main app in this viewer instance).

Any thoughts, comments or references are much appreciated,

Dave

===========================

class Picviewer:
    def __init__(self):

        print "Setting up..."
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("button-press-event", self.button_press_event )
        self.window.set_border_width(0)

        # Retrieve photos from the photo directory
        self.photo_list = self.make_photo_list()

        print "self.photo_list: ", self.photo_list

        if self.photo_list.__len__() == 0:
            print "photo_length = 0"
            dialog = gtk.Dialog("mybox", self.window.get_toplevel()  ,
gtk.DI
ALOG_MODAL | \
                     gtk.DIALOG_DESTROY_WITH_PARENT,\
                     (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
            dialog.vbox.pack_start(gtk.Label(' Error! \nNo photos found: \n\
                     Possible Cause: \n\
                     No .jpg, .gif, .png, .bmp files found. \n\
                     Picture Directory does not exist '))
            dialog.show_all()
            dialog.run()
            dialog.destroy()

<<<<not sure what to do here?>>>   del(self)  ?

        else:

            self.viewer = gtkimageview.ImageView()
            self.viewer.set_black_bg(True)
            self.viewer.set_show_frame(False)
            self.window.add(self.viewer)
            self.window.fullscreen()
            self.window.show_all()
            self.viewer.show()

  ... [stuff removed for brevity]...

    def button_press_event(self, widget, event, data=None):
        self.window.destroy()
        gobject.source_remove(self.timer_id)
        gtk.main_quit()


    def main(self):
        gtk.main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090506/49d8629b/attachment.htm 


More information about the pygtk mailing list