[pygtk] Accessing child widgets via instance variables

Dominic Salemno d.salemno at gmail.com
Sat Aug 30 01:34:02 WST 2008


Drew,

I have not used Glade... but here is a skeleton of a simple Gtk+ 
application inheriting from gtk.Window:

import gtk
import pygtk

pygtk.require('2.0')

class DocWindow(gtk.Window):

    def __init__(self):
        super(DocWindow, self).__init__()
        self.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    docWindow = DocWindow()
    docWindow.main()

Please notice the line that begins with super immediately following the 
definition of the __init__ method. This built-in function allows you to 
call the __init__ method on the 'superclass'. There are other things 
being done to merely configure and display a window. These things will 
be accomplished by calling the __init__ method on the superclass. 
Conversely, if you were to do the following:

import gtk
import pygtk

pygtk.require('2.0')

class DocWindow():

    def __init__(self):
        self.my_win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.my_win.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    docWindow = DocWindow()
    docWindow.main()

In this case you do not need to call the built-in function 'super' 
because you are not inheriting from gtk.Window but creating an instance. 
Hence, you do not need to perform the extra step needed to configure and 
display a window. I am explaining what happens in a very general manner. 
If you really wish to know how this works you may review the source code 
yourself. I hope this helps!

Sincerely, Dominic Salemno.


Drew Vogel wrote:
> If I inherit from gtk.Window as such:
>
>     class DocWindow(gtk.Window, libglade.GladeWrapper):
>
> then program exits unexpectedly without any console output so I assumed
> that pygtk did not allow this. Therefore, since my DocWindow class does
> not inherit from gtk.Window, it has no show() method of it's own. If
> this still isn't clear I will paste a test case.
>
> Drew
>
>
>
> Dominic Salemno wrote:
>   
>> Greetings,
>>
>> If my_win is already an instance of GtkWindow then to show it you merely 
>> do something along this line: my_win.show()
>>
>> You should not have to do anything afterwards. If my_win does not 
>> represent your window, what is this representing? If something else 
>> entirely, choose a different variable name. Perhaps you could paste more 
>> code for us to see?
>>
>> Your variable, if it indeed defines a GtkWindow, should be declared as 
>> follows:
>>
>>     self.my_win = gtk.Window(gtk.WINDOW_TOPLEVEL)
>>
>> Even if my_win is instantiating a custom class which in turn is 
>> inheriting from gtk.Window, you would still perform the following to 
>> show the window: self.my_win.show()
>>
>> If this is not working, please paste more code.
>>
>> Sincerely, Dominic Salemno.
>>
>>
>> Pádraig Brady wrote:
>>   
>>     
>>> Drew Vogel wrote:
>>>   
>>>     
>>>       
>>>> Thanks, I have this mostly working. However it looks like instead of calling
>>>>
>>>> my_win.show()
>>>>
>>>> I now have to call
>>>>
>>>> my_win.GtkWindow.show()
>>>>
>>>> Is this correct?
>>>>     
>>>>       
>>>>         
>>> Yes. my_win.GtkWindow is just a shortcut I thought was useful.
>>> To access other widgets, just use my_win.id.
>>>
>>> Pádraig.
>>> _______________________________________________
>>> pygtk mailing list   pygtk at daa.com.au
>>> http://www.daa.com.au/mailman/listinfo/pygtk
>>> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>>>   
>>>     
>>>       
>> _______________________________________________
>> pygtk mailing list   pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>>   
>>     
>
>   



More information about the pygtk mailing list