From 2450a25fcd146ddaaec6b0f1e962c482396f372d Mon Sep 17 00:00:00 2001 From: Jack Jansen Date: Fri, 3 Dec 1999 15:15:28 +0000 Subject: [PATCH] New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip module. --- Mac/Demo/example1.html | 115 +++++++++------------ Mac/Demo/example1/InterslipControl-1.py | 92 ----------------- Mac/Demo/example1/InterslipControl-1.rsrc | Bin 649 -> 0 bytes Mac/Demo/example1/dnslookup-1.gif | Bin 0 -> 2570 bytes Mac/Demo/example1/dnslookup-1.py | 61 ++++++++++++ Mac/Demo/example1/dnslookup-1.rsrc | Bin 0 -> 604 bytes Mac/Demo/example2.html | 98 ++++++++---------- Mac/Demo/example2/InterslipControl-2.py | 116 ---------------------- Mac/Demo/example2/InterslipControl-2.rsrc | Bin 3423 -> 0 bytes Mac/Demo/example2/dnslookup-2.gif | Bin 0 -> 2984 bytes Mac/Demo/example2/dnslookup-2.py | 92 +++++++++++++++++ Mac/Demo/example2/dnslookup-2.rsrc | Bin 0 -> 707 bytes 12 files changed, 241 insertions(+), 333 deletions(-) delete mode 100644 Mac/Demo/example1/InterslipControl-1.py delete mode 100644 Mac/Demo/example1/InterslipControl-1.rsrc create mode 100644 Mac/Demo/example1/dnslookup-1.gif create mode 100644 Mac/Demo/example1/dnslookup-1.py create mode 100644 Mac/Demo/example1/dnslookup-1.rsrc delete mode 100644 Mac/Demo/example2/InterslipControl-2.py delete mode 100644 Mac/Demo/example2/InterslipControl-2.rsrc create mode 100644 Mac/Demo/example2/dnslookup-2.gif create mode 100644 Mac/Demo/example2/dnslookup-2.py create mode 100644 Mac/Demo/example2/dnslookup-2.rsrc diff --git a/Mac/Demo/example1.html b/Mac/Demo/example1.html index ada669b29d..03c99789b8 100644 --- a/Mac/Demo/example1.html +++ b/Mac/Demo/example1.html @@ -1,5 +1,6 @@ Using python to create Macintosh applications, part one +

Using python to create Macintosh applications, part one


@@ -8,34 +9,26 @@ application using Python. We will glance at how to use dialogs and resources.

The example application we look at will be a simple program with a -dialog that allows you to control and monitor InterSLIP, a device -driver that connects your mac to the Internet via a modem connection. -Source and resource file -(in binary and BinHex form for -downloading) for this application are available in the source code and +resource file +for this application are available in the example1 folder (which you will have to download if you are reading this document over the net and if you want to look at the resources).

-We will use a C extension module module "interslip" that allows a -Python program to control and monitor the behaviour of the low-level -driver, and we will create the user interface around that. If you want -to actually run the code, you will obvously need InterSLIP and the -interslip module. The latter is available as a dynamically loadable -extension for PowerPC/cfm68k Pythons, and may be compiled in your static 68K -Python. As of this writing there is still a slight -problem with the Python interslip module causing it to say "file not -found" if the driver is not loaded yet. The workaround is to load the -driver by starting InterSLIP Control and quitting it.

+We will use the builtin module "socket" that allows a +Python program to perform all sorts of networking functions, and we +will create the user interface around that. You should be able to run +the sample code with the standard Python distribution.

If you are interested in building your own extensions to python you should check out the companion document Creating Macintosh Python C extensions, -which tells you how to build your own C extension. Not completely -coincidental this document uses the interslip module that we will use -here as an example.

+which tells you how to build your own C extension. +

Creating dialog resources

@@ -62,19 +55,24 @@ conflicts arise: once you have opened your resource file any attempt by the interpreter to open a dialog will also search your resource file.

-Okay, let's have a look at InterslipControl-1.rsrc, our resource file. +Okay, let's have a look at dnslookup-1.rsrc, our resource file. The DLOG and accompanying DITL resource both have number 512. Since ResEdit creates both with default ID=128 you should take care to -change the number on both. The dialog itself is pretty basic: four -buttons (connect, disconnect, update status and quit), two labels and -two status fields.

+change the number on both. The dialog itself is pretty basic: two +buttons (Lookup and Quit), two labels and +two text entry areas, one of which is used for output only. Here's what +the dialog will look like at run time

+

+dialog image +
+

An application with a modal dialog

Next, we will have to write the actual application. For this example, we will use a modal dialog. This means that we will put up the dialog and go into a loop asking the dialog manager for events (buttons -pushed). We handle the actions requested by the user until the quit +pushed). We handle the actions requested by the user until the Quit button is pressed, upon which we exit our loop (and the program). This way of structuring your program is actually rather antisocial, since you force the user to do whatever you, the application writer, happen @@ -86,8 +84,8 @@ answer because the data you needed was obscured by the dialog itself? In the next example we will look at an application that does pretty much the same as this one but in a more user-friendly way.

-On to the code itself, in file InterslipControl-1.py. Have +The code itself is contained in the file dnslookup-1.py. Have a copy handy before you read on. The file starts off with a textstring giving a short description. Not many tools do anything with this as yet, but at some point in the future we will have all @@ -122,39 +120,29 @@ as in print Res.OpenResFile.__doc__ Similarly, Dlg is an interface to the Dialog manager (with Dialogs being implemented as python objects and routines with Dialog arguments being methods). The sys module you -know, I hope. Interslip, -finally, is the module with the interface to the InterSLIP driver. We -use four calls from it: +know, I hope. The string module is an often used module that enables +you to perform many string related operations. In this case however, we +are only using the "digits" constant from the string module. We could +have simply defined "digits" as "0123456789". The socket module enables +us to perform the domain name lookups. We +use two calls from it:

Next in the source file we get definitions for our dialog resource number and for the item numbers in our dialog. These should match the -situation in our resource file InterslipControl-1.rsrc, -obviously. Then we get an array converting numeric state codes -returned by interslip.status() to textual messages.

+situation in our resource file dnslookup-1.rsrc, +obviously.

On to the main program. We start off with opening our resource file, which should live in the same folder as the python source. If we cannot open it we use EasyDialogs to print a message and exit. You can try it: just move the resource file somewhere else for a -moment. Then, we try to open the interslip driver, again catching an -error. All modules that raise MacOS error -exceptions will pass a 2-tuple to the exception handler with the -first item being the numeric OSErr code and the second -one being an informative message. If no informative message is -available it will be the rather uninformative "MacOS Error --12345", but at least the second item will always be a -printable string. Finally we call do_dialog() to do the real work.

+moment. Then we call do_dialog() to do the real work.

Do_dialog() uses Dlg.GetNewDialog() to open a dialog window initialized from 'DLOG' resource ID_MAIN and putting @@ -166,13 +154,12 @@ slightly more complicated things also, like the user typing into simple textfields, but it will not do things like updating the physical appearance of radio buttons, etc. See Inside Mac or another programming guide for how to handle this -yourself. Fortunately, our simple application doesn't have to bother -with this, since buttons are the only active elements we have. So, we -do a simple switch on item number and call the appropriate routine to -implement the action requested. Upon the user pressing "quit" we -simply leave the loop and, hence, do_dialog(). This will -cause the python dialog object my_dlg to be deleted and -the on-screen dialog to disappear.

+yourself. Fortunately, our simple application doesn't have to bother with this, +since buttons and textfields are the only active elements we have. So, we do a +simple switch on item number and call the appropriate routine to implement the +action requested. Upon the user pressing "Quit" we simply leave the loop and, +hence, do_dialog(). This will cause the python dialog object +my_dlg to be deleted and the on-screen dialog to disappear.

Time for a warning: be very careful what you do as long as a dialog is on-screen. Printing something, for @@ -183,8 +170,8 @@ may or may not work in this situation. I have also seen crashes in such a situation, probably due to the multiple event loops involved or some oversight in the interpreter. You have been warned.

-The implementation of the "update status" command can use a bit more -explaining: we get the new information with do_status() +The implementation of the "Lookup" command can use a bit more +explaining: we get the necessary information with dnslookup() but now we have to update the on-screen dialog to present this information to the user. The GetDialogItem() method of the dialog returns three bits of information about the given item: its @@ -195,18 +182,12 @@ here that python programmers need not bother with the C-string versus pascal-string controversy: the python glue module knows what is needed and converts the python string to the correct type.

-Finally, the three implementation routines do_connect(), -do_disconnect() and do_status() are simply -boring wrappers around the corresponding interslip methods that will -put up a dialog in case of an error.

- And that concludes our first example of the use of resources and dialogs. Next, you could have a look at the source of EasyDialogs for some examples of using input fields and filterprocs. Or, go on with reading the second part of this document -to see how to implement a better version of this application. Not only -will it allow the user to go back to the finder (or other apps) when -your application is running, it will also free her of the RSI-inducing -chore of pressing "update status" continuously...

+to see how to implement a better version of this application.

+ + diff --git a/Mac/Demo/example1/InterslipControl-1.py b/Mac/Demo/example1/InterslipControl-1.py deleted file mode 100644 index 14fc59c86c..0000000000 --- a/Mac/Demo/example1/InterslipControl-1.py +++ /dev/null @@ -1,92 +0,0 @@ -"""Sample program handling InterSLIP control and showing off EasyDialogs, -Res and Dlg in the process""" - -import EasyDialogs -import Res -import Dlg -import sys -import interslip -# -# Definitions for our resources -ID_MAIN=512 - -ITEM_CONNECT=1 -ITEM_DISCONNECT=2 -ITEM_UPDATE=3 -ITEM_QUIT=4 -ITEM_STATUS=5 -ITEM_MESSAGE=6 - -status2text = ["", "", "", "", - "", ""] - - -def main(): - """Main routine: open resourcefile, open interslip, call dialog handler""" - try: - Res.OpenResFile("InterslipControl-1.rsrc") - except Res.Error, arg: - EasyDialogs.Message("Cannot open resource file InterslipControl-1.rsrc: "+ - arg[1]) - sys.exit(1) - try: - interslip.open() - except interslip.error, arg: - EasyDialogs.Message("Cannot open interslip: "+arg[1]) - sys.exit(1) - do_dialog() - -def do_dialog(): - """Post dialog and handle user interaction until quit""" - my_dlg = Dlg.GetNewDialog(ID_MAIN, -1) - while 1: - n = Dlg.ModalDialog(None) - if n == ITEM_CONNECT: - do_connect() - elif n == ITEM_DISCONNECT: - do_disconnect() - elif n == ITEM_UPDATE: - status, msg = do_status() - - # Convert status number to a text string - try: - txt = status2text[status] - except IndexError: - txt = ""%status - - # Set the status text field - tp, h, rect = my_dlg.GetDialogItem(ITEM_STATUS) - Dlg.SetDialogItemText(h, txt) - - # Set the message text field - tp, h, rect = my_dlg.GetDialogItem(ITEM_MESSAGE) - Dlg.SetDialogItemText(h, msg) - elif n == ITEM_QUIT: - break - -def do_connect(): - """Connect, posting error message in case of failure""" - try: - interslip.connect() - except interslip.error, arg: - EasyDialogs.Message("Cannot connect: "+arg[1]) - -def do_disconnect(): - """Disconnect, posting error message in case of failure""" - try: - interslip.disconnect() - except interslip.error, arg: - EasyDialogs.Message("Cannot disconnect: "+arg[1]) - -def do_status(): - """Get status as (state_index, message), - posting error message in case of failure""" - try: - status, msgnum, msg = interslip.status() - except interslip.error, arg: - EasyDialogs.Message("Cannot get status: "+arg[1]) - return 0, '' - return status, msg - - -main() diff --git a/Mac/Demo/example1/InterslipControl-1.rsrc b/Mac/Demo/example1/InterslipControl-1.rsrc deleted file mode 100644 index 7d290a851e9cf57bbbb0c98450f1cff6d3ac4552..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 649 zcmZuv?P?Q26g|_fqz$ML(b7`FAV|@)F}s^kjcNVpY-_v~TWj*eAEn!5QvyrU?rz_q z__Gh;kBcCP4txo*{~c@@w0953Z_%|Q}G zao9iJ7!HzX7_Qjmv`(D(VHiXofQzbE78CJH=j6xOn_6-usv0$oiiWM>Xt)K}1*%%r zw5n)jYvpL=(mkg8Ob?h!Ov_9wOl78rw952|=`mBC=?PPVX^m-}=_%7QrsqsAm^PRU zlgG5lw8iw2smb(;Nh1Yt8wYUkO`St2q}{k^`A6h3){yq@pl#*kmj^*7$wV~pUewQ4 z-t^#gIip9ESulbptrJL;}@ZOhs{?T^#sCCh?posIpXu(E1TSYGNb zpmmnp%Tm<+>`+nEvZyn!gE;n&g1U@PjK3qqX!+-@Um`7H6UuDA*^+(1G~aE@TD*Ks cqtYkY-xO^gqf&TdGEZ&#emFe3q}`1E0Z`U~!vFvP diff --git a/Mac/Demo/example1/dnslookup-1.gif b/Mac/Demo/example1/dnslookup-1.gif new file mode 100644 index 0000000000000000000000000000000000000000..3cd70b2a1d009d47993afda5c3f72d6176efa355 GIT binary patch literal 2570 zcmbu8`8!qr7RNtzZ<8o0Ly-*05J%aBZzWffA>wO}24u_dCFdA2l%6I-87g6$3nv{z zbWE8VlyP4fiYQ8hDXEAKb?m#gf8l1?UL^w|17=a^tU7&y@h7%Y@U7nFn1 zuq{{#egGzPgB`#G3&Vy$4PpQdoqz=#Fc`E!8qp`Bt04_BJOmd5BVj)n3lWCskAwDg z@!oWD@r1O^i&vp*7mcP}U-XV{U9^Kf!s3Rs(jqi{lSK|%cu^WY2e=ZL2Rlt7G=NYI zLL7vm5DG@f79m4~gKceX4Gj(D%MF9?W<(e zdd{l?2d>vZYk5I0+m08fCvTQnE3WamEd`4xFMP-yd10b^QQEv(6Lqq!3o@^TmVfEJ zn~3U&YF*E2^pVE3A!Jy+!(n&xsGsH|2$jYeQJW?Kx{B8FkgZ<2~FLjm32} zUXz3ThWO>`^|koaa5r~XrbAs=$LPB@b*YGYe*Mh1FZa_T9z|5~`HC_}maI9IZNE%9 z|AgHt#^`%%Aw8wre~Vb0avVE(|?XzkRL?i(hp%kpgXo?bqg7-qi8 z_Uux%SMGN9u0(Q@@Fvff$tJf$Y*Spj4zR7Ar;Ix+SbD2+&5sX9D-u2x%Ac>A%jK~% zgT$&kGp~$CpRznV@2QY|Mp*s24IK}aD5-L{9`gF(gp7fX&T0WF> z61%^F;nZ8y=zCJl>9v@+(($+K@voI$muu21;%u)n?8l{T6{@~<6--nmK6mo7o_B5b zb>mNNSI%wHRsLT5@5*GqtAg4S-y4Ki?0H$cqTrza^Wme{O3!Tan>gAe?Y)B8+!K4~ zLBO`G3FlUo?M=+K<~qUY0IK~@7P;&96;^xiD{}hmZpzuI!@zHZ)%i^%w#?g9oj=fN z;UO6KQtGhzOq1PlHD=F|WHPWw#O0yu0zYK;du&cd%%3q+h}_HLz=q zcj?nKv)YMZta{{UuKkujW6!pDhd(J2Z=ElD)HWYF`ps zgsuHeXKs=ev2~$V@=rLm^TWHk!OBOoOG?gDlGzJW3Xi`k`)HKBaafR6BI+H#UywN2 zxuiwBV$?rDC*$|~69!5Kj}l5Qh3m=+%-ne@IyJ&%NUarg;}K$A9Sp2?D1@26pX9$T ziS(M26<+nMLDtkL%1Cwfx|{j>0vbQG%xo<7X*eXD({zuqk%|%>vU&C||7X%C1al;s zj2ksle_V5~Jy=^G)wrwrN35HTy!hUf+;ci2jM=Itv3S>m^`HFX`|jiA^ zSehyGk4@g*l_AD<%Ws^|&~TSJklVd>%UjdP2b+}Sy916Ir~$~}p4Y3iM$JS;QE`TCsEuXJ z&jJ@!&d1gm8*ZvqkyPk-q}O;)SiM#u>FVwDg=1dZdi5@Kd;4+O=(SZ{CigEY+;x#% z;Xu8%VqcCeJgI2b*Lr03!L3)3ddM-c$#s^SEu`kI)#m6PXPft{qtMH~t-q(QdbYge z^4Cq)l5hI^XDfMe_Vj;;;jN^Trx+7~wReT53PAFDW1S-a5B2o+)t&IshyO~!ul{1~>8*Hx)FTc3eXvnyR$T$YMvt%qiJp$bW(C+^MNIn3~ literal 0 HcmV?d00001 diff --git a/Mac/Demo/example1/dnslookup-1.py b/Mac/Demo/example1/dnslookup-1.py new file mode 100644 index 0000000000..a59fb8ee99 --- /dev/null +++ b/Mac/Demo/example1/dnslookup-1.py @@ -0,0 +1,61 @@ +"""Sample program performing domain name lookups and showing off EasyDialogs, +Res and Dlg in the process""" + +import EasyDialogs +import Res +import Dlg +import sys +import socket +import string +# +# Definitions for our resources +ID_MAIN=512 + +ITEM_LOOKUP_ENTRY=1 +ITEM_RESULT=2 +ITEM_LOOKUP_BUTTON=3 +ITEM_QUIT_BUTTON=4 + + +def main(): + """Main routine: open resource file, call dialog handler""" + try: + Res.OpenResFile("dnslookup-1.rsrc") + except Res.Error: + EasyDialogs.Message("Cannot open dnslookup-1.rsrc") + sys.exit(1) + do_dialog() + +def do_dialog(): + """Post dialog and handle user interaction until quit""" + my_dlg = Dlg.GetNewDialog(ID_MAIN, -1) + while 1: + n = Dlg.ModalDialog(None) + if n == ITEM_LOOKUP_BUTTON: + tp, h, rect = my_dlg.GetDialogItem(ITEM_LOOKUP_ENTRY) + txt = Dlg.GetDialogItemText(h) + + tp, h, rect = my_dlg.GetDialogItem(ITEM_RESULT) + Dlg.SetDialogItemText(h, dnslookup(txt)) + elif n == ITEM_QUIT_BUTTON: + break + +def dnslookup(str): + """ Perform DNS lookup on str. If first character of digit is numeric, + assume that str contains an IP address. Otherwise, assume that str + contains a hostname.""" + if str == '': str = ' ' + if str[0] in string.digits: + try: + value = socket.gethostbyaddr(str)[0] + except: + value = 'Lookup failed' + else: + try: + value = socket.gethostbyname(str) + except: + value = 'Lookup failed' + return value + +main() + diff --git a/Mac/Demo/example1/dnslookup-1.rsrc b/Mac/Demo/example1/dnslookup-1.rsrc new file mode 100644 index 0000000000000000000000000000000000000000..2514f5604fe80376e72897bd4a7ee8851789c548 GIT binary patch literal 604 zcmZQz6=PsvV!#BbwgX}XAm#$%I3Q*LVvzbI1_s7oMa4zQLBXyrj0_Ax49|h0 zSOHL$fpIpF0O9yOK!T}+X-i}H!2^ux0Zz;H^%ywx+5FaWt`d+>vvcB0$t%vu&(AI` z&;uD?P+63kmz-K$k(rlXTvU=#%*23XD+>b;&^R=(ZeFY;M2KnT2cWsLWaIxm4sy@r z*q8V3UOvNri8@B7bbB2}0l&2zt8C-{|Js-L|5siWNW>%W{|9?^p!g=WHH;MyRr-4W zfu?ZmvXEC>_wS9DOPY^u4Flu9JE*d@@&9gF>HWKtpyae1WD-c1y$u7SAJ8Q^46I=H zs55vnSTc$TKpd9<7V#Oe_pw!~h9FRJ71{VechA)ii42+<#2LUDqA(#Bz#LPSezr@^B1(+qM zQNz4HV~Ob>plTTgHwH!*AAffsTgb&T#0N;r0b`vPNEZMxJjF2YPjBFX#Fk5DVorWK Kw=c*J2pa&@%!t4M literal 0 HcmV?d00001 diff --git a/Mac/Demo/example2.html b/Mac/Demo/example2.html index 95e2d0a1e8..c8dde76daf 100644 --- a/Mac/Demo/example2.html +++ b/Mac/Demo/example2.html @@ -6,52 +6,51 @@ In this document we rewrite the application of the previous example to use modeless dialogs. We will use an application framework, and we will have a look at creating -applets, standalone applications written in Python. Source and resource file (in -binary and BinHex -form for downloading) are available in the folder example2.

+applets, standalone applications written in Python. The source code and +resource file are available in the folder +example2.

Again, we start with ResEdit to create our dialogs. Not only do we -want a main dialog this time but also an "About" dialog, and we -provide the BNDL resource and related stuff that -an application cannot be without. (Actually, a python applet can be -without, see below). "Inside Mac" or various -books on macintosh programming will help here. Also, you can refer to +want a main dialog this time but also an "About" dialog. This example is less +than complete since we do not provide a BNDL resource +and related stuff that an application cannot be without. We are able to do this +when building a python applet since BuildApplet will substitute default resources +for BNDL, etc. when none are supplied (See below.) +"Inside Mac" or various +books on Macintosh programming will help here. Also, you can refer to the resource files provided in the Python source distribution for some of the python-specific points of BNDL programming: the "appletbundle.rsrc" file is what is used for creating applets if you don't provide your own resource file.

-Let's have a look at InterslipControl-2.rsrc, our resource file. First -off, there's the standard BNDL combo. I've picked 'PYTi' as signature -for the application. I tend to pick PYT plus one lower-case letter for -my signatures. The finder gets confused if you have two applications -with the same signature. This may be due to some incorrectness on the -side of "BuildApplet", I am not sure. There is one case when you -definitely need a unique signature: when you create an applet that has -its own data files and you want the user to be able to start your +When creating your own BNDL resouorces, keep in mind that the Finder gets +confused if you have more than one application with the same signature. This may be due +to some incorrectness on the side of "BuildApplet", I am not sure. There is one +case when you definitely need a unique signature: when you create an applet that +has its own data files and you want the user to be able to start your applet by double-clicking one of the datafiles.

-There's little to tell about the BNDL stuff: I basically copied the -generic Python applet icons and pasted in the symbol for -InterSLIP. The two dialogs are equally unexciting: dialog 512 is our -main window which has four static text fields (two of which we will be -modifying during runtime, to show the status of the connection) and -two buttons "connect" and "disconnect". The "quit" and "update status" -buttons have disappeared, because they are handled by a menu choice -and automatically, respectively.

+Let's have a look at dnslookup-2.rsrc, our resource file. Dialog 512 is the +main window which has one button (Lookup), two labels and +two text entry areas, one of which is used for output only. The "Quit" and +button has disappeared, because its function is handled by a menu choice. Here's +what it will look like at run time:

+

+dialog image +
+

A modeless dialog application using FrameWork

On to the source code in InterslipControl-2.py. The +HREF="example2/dnslookup-2.py">dnslookup-2.py. The start is similar to our previous example program InterSlipControl-1.py, with +HREF="example1/dnslookup-1.py">dnslookup-1.py, with one extra module being imported. To make life more simple we will use the FrameWork module, a nifty piece of code that handles -all the gory mac details of event loop programming, menubar -installation and all the other code that is the same for every mac +all the gory Mac details of event loop programming, menubar +installation and all the other code that is the same for every Mac program in the world. Like most standard modules, FrameWork will run some sample test code when you invoke it as a main program, so try it now. It will create a menu bar with an Apple menu with the about box @@ -68,7 +67,7 @@ also continue with this document. After the imports we get the definitions of resource-IDs in our resource file, slightly changed from the previous version of our -program, and the state to string mapping. The main program is also +program. The main program is also similar to our previous version, with one important exception: we first check to see whether our resource is available before opening the resource file. Why is this? Because later, when we will have @@ -77,13 +76,13 @@ the applet file and we don't need the separate resource file anymore.

Next comes the definition of our main class, -InterslipControl, which inherits +DNSLookup, which inherits FrameWork.Application. The Application class handles the menu bar and the main event loop and event dispatching. In the __init__ routine we first let the base class initialize itself, then we create our modeless dialog and finally we jump into the main loop. The main loop continues until we call self._quit, -which we will do when the user selects "quit". When we create +which we will do when the user selects "Quit". When we create the instance of MyDialog (which inherits DialogWindow, which inherits Window) we pass a reference to the application object, this reference is used to tell @@ -104,31 +103,17 @@ familiar to you from the previous example program. That do_about is called when the user selects About from the Apple menu is, again, taken care of by the __init__ routine of Application.

-Our main object finally overrides idle(), the method -called when no event is available. It passes the call on to our dialog -object to give it a chance to update the status fields, if needed.

- The MyDialog class is the container for our main window. Initialization is again done by first calling the base class -__init__ function and finally setting two local variables -that are used by updatestatus() later.

+__init__ function and finally setting the local variable +"parent."

Do_itemhit() is called when an item is selected in this dialog by the user. We are passed the item number (and the original event structure, which we normally ignore). The code is similar to the main loop of our previous example program: a switch depending on the -item selected. Connect() and disconnect() -are again quite similar to our previous example.

- -Updatestatus() is different, however. It is now -potentially called many times per second instead of only when the -user presses a button we don't want to update the display every time -since that would cause some quite horrible flashing. Luckily, -interslip.status() not only provides us with a state and -a message but also with a message sequence number. If neither state -nor message sequence number has changed since the last call there is -no need to update the display, so we just return. For the rest, -nothing has changed.

+item selected. Dnslookup() is quite similar to our previous +example.

Creating applets

@@ -158,13 +143,8 @@ resources from that file will be copied to your applet too. If there is no resource file for your script a set of default resources will be used, and the applet will have the default creator 'Pyt0'. The latter also happens if you do have a resource file but without the BNDL -combo. Actually, for our example that would -have been the most logical solution, since our applet does not have -its own data files. It would have saved us hunting for an unused -creator code. The only reason for using the BNDL in this case is -having the custom icon, but that could have been done by pasting an -icon on the finder Info window, or by providing an custon icon in your -resource file and setting the "custom icon" finder bit.

+combo. Actually, as in the present example. +

If you need slightly more control over the BuildApplet process you can double-click it, and you will get dialogs for source and @@ -178,3 +158,5 @@ interpreter still work.

That's all for this example, you may now return to the table of contents to pick another topic.

+ + diff --git a/Mac/Demo/example2/InterslipControl-2.py b/Mac/Demo/example2/InterslipControl-2.py deleted file mode 100644 index 90d57dd728..0000000000 --- a/Mac/Demo/example2/InterslipControl-2.py +++ /dev/null @@ -1,116 +0,0 @@ -import FrameWork -import EasyDialogs -import Res -import Dlg -import sys -import interslip -# -# Definitions for our resources -ID_MAIN=512 -ID_ABOUT=513 - -ITEM_CONNECT=1 -ITEM_DISCONNECT=2 -ITEM_STATUS=3 -ITEM_MESSAGE=4 - -status2text = ["", "", "", "", - "", ""] - -def main(): - try: - interslip.open() - except interslip.error, arg: - EasyDialogs.Message("Cannot open interslip: "+arg[1]) - sys.exit(1) - try: - dummy = Res.GetResource('DLOG', ID_MAIN) - except Res.Error: - try: - Res.OpenResFile("InterslipControl-2.rsrc") - except Res.error: - EasyDialogs.Message("Cannot open InterslipControl-2.rsrc: "+arg[1]) - sys.exit(1) - InterslipControl() - -class InterslipControl(FrameWork.Application): - "Application class for InterslipControl" - - def __init__(self): - # First init menus, etc. - FrameWork.Application.__init__(self) - # Next create our dialog - self.main_dialog = MyDialog(self) - # Now open the dialog - self.main_dialog.open(ID_MAIN) - # Finally, go into the event loop - self.mainloop() - - def makeusermenus(self): - self.filemenu = m = FrameWork.Menu(self.menubar, "File") - self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit) - - def quit(self, *args): - self._quit() - - def do_about(self, *args): - f = Dlg.GetNewDialog(ID_ABOUT, -1) - while 1: - n = Dlg.ModalDialog(None) - if n == 1: - return - - def idle(self, event): - "Idle routine - update status" - self.main_dialog.updatestatus() - -class MyDialog(FrameWork.DialogWindow): - "Main dialog window for InterslipControl" - def __init__(self, parent): - FrameWork.DialogWindow.__init__(self, parent) - self.last_status = None - self.last_msgnum = None - - def do_itemhit(self, item, event): - if item == ITEM_DISCONNECT: - self.disconnect() - elif item == ITEM_CONNECT: - self.connect() - - def connect(self): - try: - interslip.connect() - except interslip.error, arg: - EasyDialogs.Message("Cannot connect: "+arg[1]) - - def disconnect(self): - try: - interslip.disconnect() - except interslip.error, arg: - EasyDialogs.Message("Cannot disconnect: "+arg[1]) - - def updatestatus(self): - try: - status, msgnum, msg = interslip.status() - except interslip.error, arg: - EasyDialogs.Message("Cannot get status: "+arg[1]) - sys.exit(1) - if status == self.last_status and msgnum == self.last_msgnum: - return - self.last_status = status - self.last_msgnum = msgnum - if msgnum == 0: - msg = '' - - try: - txt = status2text[status] - except IndexError: - txt = ""%status - - tp, h, rect = self.wid.GetDialogItem(ITEM_STATUS) - Dlg.SetDialogItemText(h, txt) - - tp, h, rect = self.wid.GetDialogItem(ITEM_MESSAGE) - Dlg.SetDialogItemText(h, msg) - -main() diff --git a/Mac/Demo/example2/InterslipControl-2.rsrc b/Mac/Demo/example2/InterslipControl-2.rsrc deleted file mode 100644 index 432d547c9b49a4ba0118d73ec278935a00cb5e88..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3423 zcmc&$PfQ$T6o0eqhAkxw&`@J-%9vVXSxSM0*w)%z%4{i2Tb9zbsVQw?v($~aSeC}d z3)`3&@YHzls6BWvV@=0KNKlzn%Bq z@Av-9%$wPde0zyJv|Vc8+J$urOT((ex`yS&;`|As`pud2O#I?#L|2GG;N4Lacz1t^ z6j;iY$;lqQSMSuj^lm-aJUJN*_C$Ikosq6ccZAz745nriGwG@1`~B0Y*_r97Bi$Xm z&TOBqosZ0XLgZH6u^61+#&7*9vHHy*q(`G(jXE{z(x_XbU~@1?)T1fAn$oE$U7FIZ zDM90qX&g3IBL9Z8pllIxM`d)jW{afNQUV0N zKYHa-5;Wz^#mE^EkQwa1a8PRat?i3O&j|~3d>cI(@_NIh`!$MLjWnY9DZ1vR=%R=A zSPG3=lo?oW2oJ9}X_xJ`d((FK?64ivmy6-kbH%p1bHyf4D{Jr`VKg6@I~olfx^_RA zxF1d)%4Fu)U5o%4Zp6L!`!NEI=wCzKFgnP{`ZcIuyGXjaMloj52wSHpv$2Y_2a_?% zSZ08+#bL(Qt}|xMF}99nV`_>xGOS`N>=67|7S`Gv_*kfq$qE&TT5TeA9Vc%Q)vTfJ zqO31w5^7m?8pR49qY7ht6r;d40uOM400*`acuGcDh=8{dWXUX17QsT?bh5tFQZ~D> zv9S|j2YgEp=#H1qmI_q$AmX>;6&d)sNf<5225e@R3BXwuURVHYq2Qhc&tJlMf(7v@ zWx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