[pygtk] Couple of easy questions

Wildemar Wildenburger wildemar at freakmail.de
Thu Dec 27 12:30:27 WST 2007


I'm not very experienced with pyGTK, but let me try to answer your 
questions with general Python wisdom ;)

Micah Carrick wrote:
> 1. When using gtk.Builder (or glade), in C we use g_object_unref() on 
> the builder object to free memory when we've moved references to the 
> widgets we need into variables (don't need the XML sitting around). 
> Obviously python will handle memory for me, however, do I need to set 
> the variable to None or something so it knows I'm done with it?
>
> Example:
>
> self.builder = gtk.Builder()
> self.builder.add_from_file("ui.xml")
> self.window = self.builder.get_object("window")
> dic = { "on_window_destroy" : gtk.main_quit }     
> self.builder.connect_signals(dic)
> # I'm done with the builder... is it cleaned up automatically?
>
Yep. It'll get cleaned up as soon as there are no more references to it. 
You could say "del self.builder", but that would be weird. If you only 
ever use the builder in the constructor, just don't make it an instance 
variable (that is, ommit the "self."), and it will get cleaned up upon 
returning from __init__.


> 2. What's the difference between importing pygtk and just gtk? Is that 
> a distribution difference? Just 'import gtk' works for me... but I 
> need this code to be nice and universal.
>
No clue here, sorry.


> 3. I was looking at some PyGTK projects and noticed some have 
> everything all in one .py file. Is it common convention to put 
> everything into a single file, put each class into it's own file, or 
> does it vary from programmer to programmer?
>
It is common convention to do what makes sense ;). If you have a 
relatively small program, that you want to be easily distributable, just 
put it in one file. Split it up into logical blocks if it helps 
understanding the structure of the programs (read: put related 
classes/functions in one particular file). As for creating one file per 
class: I have yet to come across a good argument for doing that ;).


have fun :)
/W


More information about the pygtk mailing list