blob: aebe73d29328912798fd99c364962ca247b4e212 [file] [log] [blame]
Jack Jansena6308131996-03-18 13:38:52 +00001<HTML><HEAD><TITLE>Using python to create Macintosh applications, part two</TITLE></HEAD>
2<BODY>
3<H1>Using python to create Macintosh applications, part two</H1>
4<HR>
5
6In this document we rewrite the application of the <A
7HREF="example1.html">previous example</A> to use modeless dialogs. We
8will use an application framework, and we will have a look at creating
9applets, standalone applications written in Python. <A
10HREF="example2/InterslipControl-2.py">Source</A> and resource file (in
11binary and <A HREF="example2/InterslipControl-2.rsrc.hqx">BinHex</A>
12form for downloading) are available in the folder <A
13HREF="example2">example2</A>. If you want to run the program on your
Jack Jansen08365421996-04-19 15:56:08 +000014machine and you have Python 1.3 or earlier you will also need a new copy of <A
Jack Jansena6308131996-03-18 13:38:52 +000015HREF="update-to-1.3/FrameWork.py">FrameWork.py</A>, which has been
16updated since the 1.3 release. <p>
17
18Again, we start with ResEdit to create our dialogs. Not only do we
19want a main dialog this time but also an "About" dialog, and we
20provide the <A NAME="bundle">BNDL resource</A> and related stuff that
21an application cannot be without. (Actually, a python applet can be
22without, <A HREF="#no-bundle">see below</A>). "Inside Mac" or various
23books on macintosh programming will help here. Also, you can refer to
24the resource files provided in the Python source distribution for some
25of the python-specific points of BNDL programming: the
26"appletbundle.rsrc" file is what is used for creating applets if you
27don't provide your own resource file. <p>
28
29Let's have a look at InterslipControl-2.rsrc, our resource file. First
30off, there's the standard BNDL combo. I've picked 'PYTi' as signature
31for the application. I tend to pick PYT plus one lower-case letter for
32my signatures. The finder gets confused if you have two applications
33with the same signature. This may be due to some incorrectness on the
34side of "mkapplet", I am not sure. There is one case when you
35definitely need a unique signature: when you create an applet that has
36its own data files and you want the user to be able to start your
37applet by double-clicking one of the datafiles. <p>
38
39There's little to tell about the BNDL stuff: I basically copied the
40generic Python applet icons and pasted in the symbol for
41InterSLIP. The two dialogs are equally unexciting: dialog 512 is our
42main window which has four static text fields (two of which we will be
43modifying during runtime, to show the status of the connection) and
44two buttons "connect" and "disconnect". The "quit" and "update status"
45buttons have disappeared, because they are handled by a menu choice
46and automatically, respectively. <p>
47
48<H2>A modeless dialog application using FrameWork</H2>
49
50On to the source code in <A
51HREF="example2/InterslipControl-2.py">InterslipControl-2.py</A>. The
52start is similar to our previous example program <A
53HREF="example1/InterslipControl-1.py">InterSlipControl-1.py</A>, with
54one extra module being imported. To make life more simple we will use
55the <CODE>FrameWork</CODE> module, a nifty piece of code that handles
56all the gory mac details of event loop programming, menubar
57installation and all the other code that is the same for every mac
58program in the world. Like most standard modules, FrameWork will run
59some sample test code when you invoke it as a main program, so try it
60now. It will create a menu bar with an Apple menu with the about box
61and a "File" menu with some pythonesque choices (which do nothing
62interesting, by the way) and a "Quit" command that works. <p>
63
Jack Jansen08365421996-04-19 15:56:08 +000064<BLOCKQUOTE>
65If you have not used <code>FrameWork</code> before you may want to
66first take a look at the <A HREF="textedit.html">Pathetic EDitor</A>
67example, which builds a minimal text editor using FrameWork and TextEdit.
68On the other hand: we don't use many features of FrameWork, so you could
69also continue with this document.
70</BLOCKQUOTE>
Jack Jansena6308131996-03-18 13:38:52 +000071
72After the imports we get the definitions of resource-IDs in our
73resource file, slightly changed from the previous version of our
74program, and the state to string mapping. The main program is also
75similar to our previous version, with one important exception: we
76first check to see whether our resource is available before opening
77the resource file. Why is this? Because later, when we will have
78converted the script to an applet, our resources will be available in
79the applet file and we don't need the separate resource file
80anymore. <p>
81
82Next comes the definition of our main class,
83<CODE>InterslipControl</CODE>, which inherits
84<CODE>FrameWork.Application</CODE>. The Application class handles the
85menu bar and the main event loop and event dispatching. In the
86<CODE>__init__</CODE> routine we first let the base class initialize
87itself, then we create our modeless dialog and finally we jump into
88the main loop. The main loop continues until <CODE>self</CODE> is
89raised, which we will do when the user selects "quit". When we create
90the instance of <CODE>MyDialog</CODE> (which inherits
91<CODE>DialogWindow</CODE>, which inherits <CODE>Window</CODE>) we pass
92a reference to the application object, this reference is used to tell
93Application about our new window. This enables the event loop to keep
94track of all windows and dispatch things like update events and mouse
95clicks. <p>
96
97The <CODE>makeusermenus()</CODE> method (which is called sometime
98during the Application <CODE>__init__</CODE> routine) creates a File
99menu with a Quit command (shortcut command-Q), which will callback to
100our quit() method. <CODE>Quit()</CODE>, in turn, raises 'self' which
101causes the mainloop to terminate. <p>
102
103Application provides a standard about box, but we override this by
104providing our own <CODE>do_about()</CODE> method which shows an about
105box from a resource as a modal dialog. This piece of code should look
106familiar to you from the previous example program. That do_about is
107called when the user selects About from the Apple menu is, again,
108taken care of by the __init__ routine of Application. <p>
109
110Our main object finally overrides <CODE>idle()</CODE>, the method
111called when no event is available. It passes the call on to our dialog
112object to give it a chance to update the status fields, if needed. <p>
113
114The <CODE>MyDialog</CODE> class is the container for our main
115window. Initialization is again done by first calling the base class
116<CODE>__init__</CODE> function and finally setting two local variables
117that are used by <CODE>updatestatus()</CODE> later. <p>
118
119<CODE>Do_itemhit()</CODE> is called when an item is selected in this
120dialog by the user. We are passed the item number (and the original
121event structure, which we normally ignore). The code is similar to the
122main loop of our previous example program: a switch depending on the
123item selected. <CODE>Connect()</CODE> and <CODE>disconnect()</CODE>
124are again quite similar to our previous example. <p>
125
126<CODE>Updatestatus()</CODE> is different, however. It is now
127potentially called many times per second instead of only when the
128user presses a button we don't want to update the display every time
129since that would cause some quite horrible flashing. Luckily,
130<CODE>interslip.status()</CODE> not only provides us with a state and
131a message but also with a message sequence number. If neither state
132nor message sequence number has changed since the last call there is
133no need to update the display, so we just return. For the rest,
134nothing has changed. <p>
135
136<H2><IMG SRC="html.icons/mkapplet.gif"><A NAME="applets">Creating applets</A></H2>
137
138Now, if you have a PowerPC Macintosh, let us try to turn the python
139script into an applet, a standalone application. Actually,
140"standalone" is probably not the correct term here, since an applet
141does still depend on a lot of the python environment: the PythonCore
142shared library, the Python Preferences file, the python Lib folder and
143any other modules that the main module depends on. It is possible to
144get rid of all these dependencies except for the dependency on
145PythonCore, but at the moment that is still quite difficult so we will
146ignore that possibility for now. By standalone we mean here that the
147script has the look-and-feel of an application, including the ability
148to have its own document types, be droppable, etc. <p>
149
150The easiest way to create an applet is to take your source file and
151drop it onto "mkapplet" (normally located in the Python home
152folder). This will create an applet with the same name as your python
153source with the ".py" stripped. Also, if a resource file with the same
154name as your source but with ".rsrc" extension is available the
155resources from that file will be copied to your applet too. If there
156is no resource file for your script a set of default resources will be
157used, and the applet will have the default creator 'PYTa'. The latter
158also happens if you do have a resource file but without the BNDL
159combo. <A NAME="no-bundle">Actually</A>, for our example that would
160have been the most logical solution, since our applet does not have
161its own data files. It would have saved us hunting for an unused
162creator code. The only reason for using the BNDL in this case is
163having the custom icon, but that could have been done by pasting an
164icon on the finder Info window, or by providing an custon icon in your
165resource file and setting the "custom icon" finder bit. <p>
166
167If you need slightly more control over the mkapplet process you can
168double-click mkapplet, and you will get dialogs for source and
169destination of the applet. The rest of the process, including locating
170the resource file, remains the same. <p>
171
172Note that though our example application completely bypasses the
173normal python user interface this is by no means necessary. Any python
174script can be turned into an applet, and all the usual features of the
175interpreter still work. <p>
176
177That's all for this example, you may now return to the <A HREF="index.html">
178table of contents</A> to pick another topic. <p>