LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-02-2010, 10:13 AM   #1
sam_o_rogers
Member
 
Registered: Apr 2006
Posts: 47
Blog Entries: 1

Rep: Reputation: 15
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
 
Old 01-02-2010, 10:41 AM   #2
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by sam_o_rogers View Post

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.
 
1 members found this post helpful.
Old 01-04-2010, 07:09 AM   #3
sam_o_rogers
Member
 
Registered: Apr 2006
Posts: 47
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Smile Using Python import whatever, dir(whatever)

Quote:
Originally Posted by GooseYArd View Post
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
 
  


Reply

Tags
gtk, pygtk, python, ubuntu


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 09:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 05:50 AM
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 04:44 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 08:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 12:05 PM


All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration