LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Py_GTK and Python (http://www.linuxquestions.org/questions/programming-9/py_gtk-and-python-779414/)

sam_o_rogers 01-02-2010 09:13 AM

Py_GTK and Python
 
I apologise if this question is answered elsewhere, but I've spent a lot of time searching without hitting the right place, so I'm asking someone's indulgence for "not reading the manual".

Here is a little background.

This is an Acer Aspire netbook with Ubuntu 9.04.

I noticed that Python is a language that is available on a number of platforms. I was also impressed with GTK that came with Gimp when I installed it on a windows machine years ago.

Trying to find a gui interface for Python, there appeared to be two good candidates. Py_QT and Py_GTK. I was able to install the IDE for Py_QT, but ran into problems when trying to run stuff. However Py_GTK seemed to install with no problem, and I have been able to follow the tutorial with no trouble.

When I did the aptitude search for gtk, I found the following:
i python-gtk2 - Python bindings for the GTK+ widget set
p python-gtk2-dbg - Python bindings for the GTK+ widget set (d
p python-gtk2-dev - GTK+ bindings: devel files
i python-gtk2-doc - Python bindings for the GTK+ widget set -
i python-gtk2-tutorial - tutorial for the GTK2 python library

After installing, I have for example the following directory:

file:///usr/share/gtk-doc/html/pygtk/gtk-class-reference.html

I have been following this tutorial:

http://zetcode.com/tutorials/pygtktutorial/

Aside from having to provide a few of my own .jpg images, all the examples seem to work.

All the examples have the following lines:

#!/usr/bin/python
import gtk

If I cd to the /usr/bin directory, I can find python with no problem.

ubuntudevelop@UbuntuNetbook:~$ cd /usr/bin
ubuntudevelop@UbuntuNetbook:/usr/bin$ ls -ld python
lrwxrwxrwx 1 root root 9 2009-08-22 23:04 python -> python2.6
ubuntudevelop@UbuntuNetbook:/usr/bin$

Now, running a small python script in the shell, when you import,
the .py at the end of the import is normally implied. For example I have the following module:

ubuntudevelop@UbuntuNetbook:~/python_work$ cat first_python_module.py
# first python module
def print_hello_world_name(user_name):
hello_world_string = "Hello "
hello_world_string = hello_world_string + user_name + '\n'
print hello_world_string

Then the file:
ubuntudevelop@UbuntuNetbook:~/python_work$ cat hello_world.py
import first_python_module

username = "Ubuntu"
first_python_module.print_hello_world_name(username)
ubuntudevelop@UbuntuNetbook:~/python_work$

Finally the command:

ubuntudevelop@UbuntuNetbook:~/python_work$ python hello_world.py
Hello Ubuntu

ubuntudevelop@UbuntuNetbook:~/python_work$

--------------------------------------------------

So, now to my questions. I know I can read the reference documentation on classes. One of the things GTK has are pre-defined STOCK items. For example there is a STOCK_QUIT.

Now I may be making an erroneous assumption, but if I have a statement:

import gtk, somewhere on my system there should be a gtk.py. I would like to simply look at this file to see what is in it. However when I do a find . -name gtk.py -print, I get no results. (I su to root, so there are no hidden directories.)

If I simply search for gtk, I get lots of hits. Most of them are directories. For example there is a file:

/usr/lib/python-support/python-gtk2/python2.6/gtk-2.0/gtk/_gtk.so

This is not a text file, but a binary shared object file. Is this the file that gets imported?

Some of the examples also use:

import sys

Where is the sys that is imported?

Is there an envrionment variable or a config file that tells python where to find these files? If so, where are these? Or does it simply have a standard location for putting these files?

After I get things working here, undoubtedly I will have similar questions about python and gtk on Macs and Windows XP. It would be awfully nice to be able to write a piece of code and have it work regardless of the machine it runs on.

Thank you in advance for your patience and assistance.

Sam

GooseYArd 01-02-2010 09:41 AM

Quote:

Originally Posted by sam_o_rogers (Post 3811639)

Now I may be making an erroneous assumption, but if I have a statement:

import gtk, somewhere on my system there should be a gtk.py. I would like to simply look at this file to see what is in it. However when I do a find . -name gtk.py -print, I get no results. (I su to root, so there are no hidden directories.)

If I simply search for gtk, I get lots of hits. Most of them are directories. For example there is a file:

/usr/lib/python-support/python-gtk2/python2.6/gtk-2.0/gtk/_gtk.so

This is not a text file, but a binary shared object file. Is this the file that gets imported?

Some of the examples also use:

import sys

Where is the sys that is imported?

Is there an envrionment variable or a config file that tells python where to find these files? If so, where are these? Or does it simply have a standard location for putting these files?

Hi Sam,

You're right on the money- some python extensions are not written in python. When you import them, you're importing a dynamically loaded module, essentially a C library, that implements an interface that the interpreter knows how to use.

I think that the core modules like sys are linked into the interpreter, I've never actually looked for them, but I suspect that even if you find where the symbols live, it'd be a little difficult to read. Since the module docs are pretty good, everything you'll need would be at:

http://docs.python.org/modindex.html

one thing you can do if you're curious is to run the interpreter (just run python with no arguments or scripts)

then say

"import whatever"

then

dir(whatever)


calling dir() on a module name will give you a directory of the things that are exported by the module. Theres no documentation per se, but often times just seeing the names of the functions is helpful to give you some distinct terms to google for.

sam_o_rogers 01-04-2010 06:09 AM

Using Python import whatever, dir(whatever)
 
Quote:

Originally Posted by GooseYArd (Post 3811652)
Hi Sam,

one thing you can do if you're curious is to run the interpreter (just run python with no arguments or scripts)

then say

"import whatever"

then

dir(whatever)


calling dir() on a module name will give you a directory of the things that are exported by the module. Theres no documentation per se, but often times just seeing the names of the functions is helpful to give you some distinct terms to google for.

Dear GooseYArd,

Thank you for your response. Running the python dir(gtk) gave me an eyeful. I noticed you have been thanked 18 times. I wanted to add to the count, but didn't see the field to do this. I did flag your response as helpful.

When I do a cd to

/usr/lib/python-support/python-gtk2/python2.6/gtk-2.0, then ls -ld, I see a file gtkunixprint.so.

Following your directions, replacing whatever with gtkunixprint I get the following.

['PRINT_CAPABILITY_COLLATE', 'PRINT_CAPABILITY_COPIES', 'PRINT_CAPABILITY_GENERATE_PDF', 'PRINT_CAPABILITY_GENERATE_PS', 'PRINT_CAPABILITY_NUMBER_UP', 'PRINT_CAPABILITY_NUMBER_UP_LAYOUT', 'PRINT_CAPABILITY_PAGE_SET', 'PRINT_CAPABILITY_PREVIEW', 'PRINT_CAPABILITY_REVERSE', 'PRINT_CAPABILITY_SCALE', 'PageSetupUnixDialog', 'PrintCapabilities', 'PrintJob', 'PrintUnixDialog', 'Printer', '__doc__', '__file__', '__name__', '__package__', '__version__', 'enumerate_printers']

I can see that this will be very helpful.

Sam


All times are GMT -5. The time now is 07:02 AM.