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

dave mcodes davemcodes at gmail.com
Thu May 7 07:58:14 WST 2009


Thanks for the reply Milosz,

The project is simple. Just a top level window with a few buttons, one to
launch a music player and the other to launch a slide show. The slide shower
is full screen. When you click on that screen, you just return to the menu.

So..If I get you correctly, you're saying that rather than manage the events
from within the viewer (or another child class) let the top level class
handle events and manage the viewer (child) upon receiving the event.

Your advice is well taken and I actually have been reading a number of
on-line tutorials for Python and OOP. I expect it will take a little while
to get the hang of it.

Dave


On Wed, May 6, 2009 at 4:16 PM, Milosz Derezynski
<internalerror at gmail.com>wrote:

> Your problem can be tackled from various angles, but it all runs down to
> this: You should not need to make the instance delete itself. More
> precisely, you should not need to make the instance aware of when it ought
> to be deleted, the fact that it should not delete itself *aside* (and the
> fact aside that Python does GC and del()-ing an instance is a rare
> exception.)
>
> As you provided only little insight into the nature of your project and
> your code, I am not sure what specifically to recommend, but it seems that
> it would be more appropriate if you had a viewer class that does the actual
> showing, and separate the file processing into a different class or free
> code outside of the viewer class, and manage the lifecycle of the viewer
> class by the main program code (the 'controller' code).
>
> In fact, to be honest, your idea of how this should work looks pretty
> jumbled up and I recommend and advise that you read up in a Python tutorial
> to get familiar with OOP programming.
>
> On Thu, May 7, 2009 at 12:55 AM, dave mcodes <davemcodes at gmail.com> wrote:
>
>> 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()
>>
>>
>> _______________________________________________
>> pygtk mailing list   pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>>
>
>
>
> --
> Please note that according to the German law on data retention,
> information on every electronic information exchange with me is
> retained for a period of six months.
> [Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
> jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090506/28198561/attachment.htm 


More information about the pygtk mailing list