Python and Glade: window not showing

S

Sven Arduwie

Can anyone help a python newbie and tell me why the simple window I
created in Glade is not showing?

This is the XML generated by Glade 3:
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="helloWorld">
<property name="visible">True</property>
<signal name="destroy" handler="on_helloWorld_destroy"/>
<child>
<placeholder/>
</child>
</object>
</interface>

And this is the Python code:
#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk

class HelloWorld(object):
def getWindow(self):
return self.window

def setWindow(self, window):
self.window = window

window = property(getWindow, setWindow)

def __init__(self):
builder = gtk.Builder()
builder.add_from_file("helloWorld.glade")
builder.connect_signals({"on_helloWorld_destroy" :
self.onHelloWorldDestroy})
self.window = builder.get_object("helloWorld")
self.window.show()

def onHelloWorldDestroy(self):
pass

I ran this in a terminal on Ubuntu 9.04 like this:
sven@Dell:~$ cd ./gvfs/python\ on\ sven/
sven@Dell:~/gvfs/python on sven$ python ./helloWorld.py
sven@Dell:~/gvfs/python on sven$
 
S

Sven Arduwie

Can anyone help a python newbie and tell me why the simple window I
created in Glade is not showing?

This is the XML generated by Glade 3:
<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="helloWorld">
    <property name="visible">True</property>
    <signal name="destroy" handler="on_helloWorld_destroy"/>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>

And this is the Python code:
#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk

class HelloWorld(object):
        def getWindow(self):
                return self.window

        def setWindow(self, window):
                self.window = window

        window = property(getWindow, setWindow)

        def __init__(self):
                builder = gtk.Builder()
                builder.add_from_file("helloWorld.glade")
                builder.connect_signals({"on_helloWorld_destroy" :
self.onHelloWorldDestroy})
                self.window = builder.get_object("helloWorld")
                self.window.show()

        def onHelloWorldDestroy(self):
                pass

I ran this in a terminal on Ubuntu 9.04 like this:
sven@Dell:~$ cd ./gvfs/python\ on\ sven/
sven@Dell:~/gvfs/python on sven$ python ./helloWorld.py
sven@Dell:~/gvfs/python on sven$

Okay I'm mad at myself for forgetting this:

if __name__ == "__main__":
helloWorld = HelloWorld()
gtk.main()

When I add that, a new problem arises: the terminal floods with:
File "./helloWorld.py", line 12, in setWindow
self.window = window
File "./helloWorld.py", line 12, in setWindow
self.window = window
File "./helloWorld.py", line 12, in setWindow
self.window = window
ad infinitum
 
D

Dave Angel

Sven said:
Okay I'm mad at myself for forgetting this:

if __name__ ="__main__":
helloWorld =elloWorld()
gtk.main()

When I add that, a new problem arises: the terminal floods with:
File "./helloWorld.py", line 12, in setWindow
self.window =indow
File "./helloWorld.py", line 12, in setWindow
self.window =indow
File "./helloWorld.py", line 12, in setWindow
self.window =indow
ad infinitum
You have infinite recursion because setWindow is defined indirectly in
terms of itself. It uses the property 'window', which is defined to use
setWindow.

The cure for it is simple. If you want to have a private data
attribute, use a leading underscore. Don't call it the same thing that
the public is going to use.

class HelloWorld(object):
def getWindow(self):
return self._window

def setWindow(self, window):
self._window = window

window = property(getWindow, setWindow)

def __init__(self):
builder = gtk.Builder()
builder.add_from_file("helloWorld.glade")
builder.connect_signals({"on_helloWorld_destroy" :
self.onHelloWorldDestroy})
self._window = builder.get_object("helloWorld")
self._window.show()

def onHelloWorldDestroy(self):
pass


(untested)
 
S

Sven Arduwie

You have infinite recursion because setWindow is defined indirectly in
terms of itself.  It uses the property 'window', which is defined to use
setWindow.

The cure for it is simple.  If you want to have a private data
attribute, use a leading underscore.  Don't call it the same thing that
the public is going to use.

class HelloWorld(object):
         def getWindow(self):
                 return self._window

         def setWindow(self, window):
                 self._window = window

         window = property(getWindow, setWindow)

         def __init__(self):
                 builder = gtk.Builder()
                 builder.add_from_file("helloWorld.glade")
                 builder.connect_signals({"on_helloWorld_destroy" :
 self.onHelloWorldDestroy})
                 self._window = builder.get_object("helloWorld")
                 self._window.show()

         def onHelloWorldDestroy(self):
                 pass

(untested)

That solved the problem, thanks!

I assume that the getWindow and setWindow can be bypassed by using the
_window property directly and that Python has no visibility keywords
like private or protected. Sort of like PHP 4. (Not that I want to
compare Python to anything like that mess, lol ;))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top