LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Getting GTK to work with python (http://www.linuxquestions.org/questions/programming-9/getting-gtk-to-work-with-python-504717/)

crazyjimbo 11-25-2006 07:04 AM

Getting GTK to work with python
 
Hi all,

I've been trying to learn how to use GTK in Python, but haven't managed to get past the 'Hello World' Example yet. Using the code below, I get the error:

'AttributeError: 'module' object has no attribute 'Window''

Code:

#!/usr/bin/env python

# example base.py

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

class Base:
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.show()

    def main(self):
        gtk.main()

print __name__
if __name__ == "__main__":
    base = Base()
    base.main()

I'm pretty sure I've got all the libraries I need installed (python-gtk2, all the gtk development libraries (I've coded in C with no problem), etc). I'm really stumped as to what is causing this error.

I'm using Debian Etch for the record.

Many Thanks,
James

ghostdog74 11-25-2006 08:50 AM

How about this?

Code:

#!/usr/bin/env python
 import gtk
 class Base:
    def __init__(self):
        self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
        self.window.show()
 def main():
    gtk.mainloop()
 
 if __name__ == "__main__":
    base = Base()
    main()


Andrew Benton 11-25-2006 09:10 AM

crazyjimbo, your code works for me without error, it opens a little window. It would seem you need to install a python package. This is the list of python packages I have installed, perhaps you need to install one of them?
pycairo-1.2.0.tar.gz
pygobject-2.11.1.tar.bz2
pygtk-2.9.0.tar.bz2
pyorbit-2.14.1.tar.bz2
PyPanel-2.4.tar.gz
Python-2.4.3.tar.bz2
python-xlib-0.12.tar.gz
PyXML-0.8.4.tar.gz

crazyjimbo 11-27-2006 11:35 AM

ghostdog74: I have tried your code and I get the same error (except it complains about GtkWindow instead of Window).

Andrew Benton: The code SHOULD work, I lifted it straight form the pygtk tutorial, but for some reason it doesn't work for me. As I said, I'm pretty sure I have all the required packages. Although, where you have pygtk, I have a package that apt (I'm in Debian Etch) calls 'python-gtk2'. I had assumed this was the same thing, as it comes up when I search for pygtk.

At this stage, I have no idea what is going on and don't know where to turn to find an answer which is most frustrating!

Many thanks,

James

crazyjimbo 11-27-2006 12:14 PM

I've installed pygtk from source from the pygtk website and everything works now. I don't whether I didn't actually have pygtk or if the Debian version just wasn't working. Anyway, problem solved, thanks for the help!


All times are GMT -5. The time now is 12:37 AM.