[Pythonmac-SIG] (no subject)

Corran Webster cwebster@nevada.edu
Wed, 10 Nov 1999 14:10:46 -0800 (PST)


On Wed, 10 Nov 1999, Craig Hagerman wrote:

> 	I have been learning Python with the O'Reilly books recently on my
> Macintosh. While their examples are done on Linux and often outside my
> experience (like server-side scripting), I have found the core language
> quite easy to understand. However, the documentation for Mac specific
> capabilities of Python seems to be sparse (both in print and on the web --
> PLEASE correct me if I am wrong here).

I think this is probably a fair observation, sadly.

> I have three simple (newbie) Mac - Python related questions if anyone
> can answer one or all. 

OK.

> 1.  The Mac pdf documentation doesn't make it clear to me what Pythons
> capabilities and limitations are for Mac specific programming. Perhaps
> someone could help clarify this for me with a comparison. I know that
> AppleScript and Python are different beasts but it would be helpful to
> understand ways that they are similar; as in what kinds of things could I
> do with either language, and how they are different; ie. what kinds of
> things could I do in AppleScript and NOT in Python?

There is additional HTML documentation in the :Mac:Demo folder of the
distribution which will answer some of your questions.  But yes, the
documentation could be better, particularly for newbies.

To answer your particular questions:

* The limitations of Mac Python tend to come from the differences in the
underlying operating system - anything on Unix which would call an
external command, or open a pipe, or the like, will not work on the Mac
without serious modification.  Also thread support has not been
incorporated yet (but is now on the horizon as a possibility).  This tends
to limit Python's applicability as a "glue" language between other,
separate programs, which is one of the uses of Python in Unix.

* Applescript is such a "glue" language on the Mac, but without extra
add-ons, is fairly limited to that role.  Python is a much more
general-purpose language than Applescript.  Of course, pretty much
anything Applescript can do, Python can as well, since it has the hooks
into the Apple Event system of the OS, but it's not as natural as doing it
in Applescript.  And with the right plug-ins, Applescript can probably do
anything Python can do.  Basically it's just a question of differences in
focus.

With python you can do just about anything a regular Mac application can
do, and you can do it more easily than you could using C or C++.
Unfortunately, it's still not easy.

> 2.  I don't mind learning at the command line but I would be far more
> comfortable dealing with a GUI as the end result. I understand that Tkinter
> has some issues on the Mac. At the risk of asking a really stupid
> question... would it be possible to use RealBasic to create a GUI but use
> Python for the scripting and Apple Events to communicate between the two. I
> suspect the answer is no but I think it would be great if I could.

If you like learning with a GUI, I'd suggest working with the Python IDE.
It has a nice widget set which is accessible from Python.  Unfortunately
it isn't well documented.  You might look into the PIDDLE graphics system
<http://www.strout.net/python/> since the QuickDraw module allows you to
draw graphics in an IDE window interactively.

On a more general level, the following commands in the IDE will get you
started:

import W

w = W.Window((600, 400), "Hello!", minsize = (240, 200))

def buttonCallback():
    print "Hello World!"

w.button = W.Button((20,20,100,50), "Hello World!", buttonCallback)

w.open()

More complex examples can be found in the IDE's Scripts menu - the source
code of the examples should be easy to follow..

It'd prolly be a good idea if these were better advertised, because it
does make the IDE quite a nice learning environment. 

> 3.  My understanding is that it is possible to use Python to create simple
> applications on the Mac. (Or is this a misunderstanding - it is possible to
> create applications with Python AND C together ??) I am interested in doing
> this to learn more about (a) OO programming and (b) Mac programming.
> (Python was the OVERWHELMING recommendation when I asked sources what to
> learn as a first step into OOP.)

It's actually possible to create quite complex applications on the Mac
using Python - the IDE is written entirely in Python, for example.

> 	As I have said I have found the "mac.pdf" documentation of the Mac
> Modules a bit minimalistic for my understanding and can't find much else.
> The "Toolbox" (pdf download) of "Inside Macintosh" helps but not that much
> for a beginner like me. It is a big question but I want to know HOW to
> program Python on a Macintosh to create a simple application. I am assuming
> a GUI creation here - drawing Windows, Buttons and other controls,
> interacting with other applications, catching events etc.

The starting point for a Mac application is the Framework module
documented in the "mac.pdf" documentation.  The "W" extensions to
Framework used in the IDE are a better starting point, but aren't
documented yet.

But if you want to program a GUI application on the Macintosh, you have
two options: either you will need to learn about the underlying Macintosh
GUI commands via Inside Macintosh (and Python is as good a place as any to
do this, since it has a nice interface to the Toolbox calls and you can
work interactively); or you will have to use Tkinter, which is fine for
learning, but I'd hesitate to create a full-blown mac application with it.

> 	Is there any other books, documentation, sources, whatever that can
> help me with this quest?? I suppose that non-Python Mac instruction sources
> would be better than nothing. There just doesn't seem to be that much Mac
> specific literature out there so...  As another avenue of learning I am
> wondering if there are any (Mac)Python Wizards out there who have made such
> Mac applications and would allow me to pour over their source code for some
> examples???? If so please email me. If I got enough example code I would be
> willing to set up a web site dedicated to hosting (and growing) those
> examples.

Check in the :Mac:Demo folder of the distribution - many of your questions
may be answered there, and there are some simple demo applications which
you can poke around inside and work out what makes them tick.  For a more
complex example, you can have a look at the IDE's source code in the
:Mac:Tools:IDE folder.

For Tkinter examples, have a look at the :Demo:tkinter folder of the
distribution for examples - some of them don't work because they rely on
Unix features or threads, but some do, and looking at how they work could
be enlightening.  For documentation, check <http://www.python.org/doc/>
for various guides to Tkinter - most stuff is standard cross-platform.

You might also like to subscribe to the Python Tutor mailing list - it's
not mac-specific, but it may help you with basic questions you come up
with when learning Python.

I hope this has helped answer some of your questions.

Regards,
Corran