annotate Mac/Demo/example1.html @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents cc412dfd79f7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1 <HTML><HEAD><TITLE>Using python to create Macintosh applications, part one</TITLE></HEAD>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
2 <BODY>
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
3
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 <H1>Using python to create Macintosh applications, part one</H1>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 <HR>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7 This document will show you how to create a simple mac-style
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8 application using Python. We will glance at how to use dialogs and
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
9 resources. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
10
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
11 The example application we look at will be a simple program with a
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
12 dialog that allows you to perform domain name lookups on IP addresses
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
13 and hostnames.
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
14 The <A HREF="example1/dnslookup-1.py">source code</A> and
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
15 <A HREF="example1/dnslookup-1.rsrc">resource file</A>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
16 for this application are available in the <A
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
17 HREF="example1">example1</A> folder (which you will have to download
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
18 if you are reading this document over the net and if you want to look
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
19 at the resources). <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
21 We will use the builtin module "socket" that allows a
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
22 Python program to perform all sorts of networking functions, and we
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
23 will create the user interface around that. You should be able to run
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
24 the sample code with the standard Python distribution.<p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
26 <CITE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 If you are interested in building your own extensions to python you
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
28 should check out the companion document <A
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
29 HREF="plugins.html">Creating Macintosh Python C extensions</A>,
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
30 which tells you how to build your own C extension.
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
31 <p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
32 </CITE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
33
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34 <H2><A NAME="dialog-resources">Creating dialog resources</A></H2>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36 Let us start with the creative bit: building the dialogs and creating
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 an icon for our program. For this you need ResEdit, and a reasonable
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
38 working knowledge of how to use it. "Inside Mac" or various books on
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
39 macintosh programming will help here. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
40
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41 There is one fine point that deserves to be mentioned here: <A
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42 NAME="resource-numbering">resource numbering</A>. Because often your
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
43 resources will be combined with those that the Python interpreter and
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44 various standard modules need you should give your DLOG and DITL
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
45 resources numbers above 512. 128 and below are reserved for Apple,
7468
149a54798896 Added (minimal) documentation on how to build standalone applications
Jack Jansen <jack.jansen@cwi.nl>
parents: 3457
diff changeset
46 128-228 are for extensions like Tk,
149a54798896 Added (minimal) documentation on how to build standalone applications
Jack Jansen <jack.jansen@cwi.nl>
parents: 3457
diff changeset
47 228-255 for the Python interpreter and 256-511 for standard
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
48 modules. If you are writing a module that you will be distributing for
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
49 inclusion in other people's programs you may want to register a number
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
50 in the 256-511 range, contact Guido or myself or whoever you think is
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
51 "in charge" of Python for the Macintosh at the moment. Even though the
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
52 application we are writing at the moment will keep its resources in a
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
53 separate resource file it is still a good idea to make sure that no
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
54 conflicts arise: once you have opened your resource file any attempt
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
55 by the interpreter to open a dialog will also search your resource
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
56 file. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
57
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
58 Okay, let's have a look at dnslookup-1.rsrc, our resource file.
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
59 The DLOG and accompanying DITL resource both have number 512. Since
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
60 ResEdit creates both with default ID=128 you should take care to
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
61 change the number on both. The dialog itself is pretty basic: two
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
62 buttons (Lookup and Quit), two labels and
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
63 two text entry areas, one of which is used for output only. Here's what
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
64 the dialog will look like at run time<p>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
65 <div align=center>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
66 <img width=324 height=189 src="example1/dnslookup-1.gif" alt="dialog image">
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
67 </div>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
68 <p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
69
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
70 <H2><A NAME="modal-dialog">An application with a modal dialog</A></H2>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
71
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
72 Next, we will have to write the actual application. For this example,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
73 we will use a modal dialog. This means that we will put up the dialog
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
74 and go into a loop asking the dialog manager for events (buttons
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
75 pushed). We handle the actions requested by the user until the Quit
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
76 button is pressed, upon which we exit our loop (and the program). This
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
77 way of structuring your program is actually rather antisocial, since
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78 you force the user to do whatever you, the application writer, happen
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
79 to want. A modal dialog leaves no way of escape whatsoever (except
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
80 command-option-escape), and is usually not a good way to structure
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
81 anything but the most simple questions. Even then: how often have you
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
82 been confronted with a dialog asking a question that you could not
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
83 answer because the data you needed was obscured by the dialog itself?
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
84 In the next example we will look at an application that does pretty
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
85 much the same as this one but in a more user-friendly way. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
86
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
87 The code itself is contained in the file <A
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
88 HREF="example1/dnslookup-1.py"> dnslookup-1.py</A>. Have
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
89 a copy handy before you read on. The file starts off with a
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
90 textstring giving a short description. Not many tools do anything with
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
91 this as yet, but at some point in the future we <EM>will</EM> have all
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
92 sorts of nifty class browser that will display this string, so just
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
93 include it. Just put a short description at the start of each module,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
94 class, method and function. After the initial description and some
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
95 comments, we import the modules we need. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
96
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
97 <A NAME="easydialogs"><CODE>EasyDialogs</CODE></A> is a handy standard
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
98 module that provides you with routines that put up common text-only
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
99 modal dialogs:
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
100 <UL>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
101 <LI> <CODE>Message(str)</CODE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
102 displays the message "str" and an OK button,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
103 <LI> <CODE>AskString(prompt, default)</CODE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
104 asks for a string, displays OK and Cancel buttons,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
105 <LI> <CODE>AskYesNoCancel(question, default)</CODE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
106 displays a question and Yes, No and Cancel buttons.
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
107 </UL>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
108
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
109 <A NAME="res"><CODE>Res</CODE></A> is a pretty complete interface to
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
110 the MacOS Resource Manager, described fully in Inside Mac. There is
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
111 currently no documentation of it, but the Apple documentation (or
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
112 Think Ref) will help you on your way if you remember two points:
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
113 <UL>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
114 <LI> Resources are implemented as Python objects, and each routine
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
115 with a resource first argument is implemented as a python method.
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
116 <LI> When in doubt about the arguments examine the routines docstring,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
117 as in <CODE>print Res.OpenResFile.__doc__</CODE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
118 </UL>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
119
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
120 Similarly, <A NAME="dlg"><CODE>Dlg</CODE></A> is an interface to the
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
121 Dialog manager (with Dialogs being implemented as python objects and
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
122 routines with Dialog arguments being methods). The sys module you
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
123 know, I hope. The string module is an often used module that enables
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
124 you to perform many string related operations. In this case however, we
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
125 are only using the "digits" constant from the string module. We could
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
126 have simply defined "digits" as "0123456789". The socket module enables
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
127 us to perform the domain name lookups. We
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
128 use two calls from it:
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
129 <UL>
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
130 <LI> <CODE>gethostbyaddr()</CODE>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
131 returns the hostname associated with an IP address
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
132 <LI> <CODE>gethostbyname()</CODE>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
133 returns the IP address associated with a hostname
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
134 </UL>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
135
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
136 Next in the source file we get definitions for our dialog resource
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
137 number and for the item numbers in our dialog. These should match the
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
138 situation in our resource file dnslookup-1.rsrc,
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
139 obviously.<p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
140
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
141 On to the main program. We start off with opening our resource file,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
142 which should live in the same folder as the python source. If we
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
143 cannot open it we use <CODE>EasyDialogs</CODE> to print a message and
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
144 exit. You can try it: just move the resource file somewhere else for a
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
145 moment. Then we call do_dialog() to do the real work. <p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
146
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
147 <CODE>Do_dialog()</CODE> uses <CODE>Dlg.GetNewDialog()</CODE> to open
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
148 a dialog window initialized from 'DLOG' resource ID_MAIN and putting
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
149 it on screen in the frontmost position. Next, we go into a loop,
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
150 calling <CODE>Dlg.ModalDialog()</CODE> to wait for the next user
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
151 action. <CODE>ModalDialog()</CODE> will return us the item number that
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
152 the user has clicked on (or otherwise activated). It will handle a few
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
153 slightly more complicated things also, like the user typing into
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
154 simple textfields, but it will <EM>not</EM> do things like updating
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
155 the physical appearance of radio buttons, etc. See Inside Mac or
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
156 another programming guide for how to handle this
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
157 yourself. Fortunately, our simple application doesn't have to bother with this,
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
158 since buttons and textfields are the only active elements we have. So, we do a
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
159 simple switch on item number and call the appropriate routine to implement the
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
160 action requested. Upon the user pressing "Quit" we simply leave the loop and,
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
161 hence, <CODE>do_dialog()</CODE>. This will cause the python dialog object
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
162 <CODE>my_dlg</CODE> to be deleted and the on-screen dialog to disappear. <p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
163
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
164 <A NAME="dialog-warning">Time for a warning</A>: be very careful what
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
165 you do as long as a dialog is on-screen. Printing something, for
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
166 instance, may suddenly cause the standard output window to appear over
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
167 the dialog, and since we took no measures to redraw the dialog it will
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
168 become very difficult to get out of the dialog. Also, command-period
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
169 may or may not work in this situation. I have also seen crashes in
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
170 such a situation, probably due to the multiple event loops involved or
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
171 some oversight in the interpreter. You have been warned. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
172
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
173 The implementation of the "Lookup" command can use a bit more
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
174 explaining: we get the necessary information with <CODE>dnslookup()</CODE>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
175 but now we have to update the on-screen dialog to present this
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
176 information to the user. The <CODE>GetDialogItem()</CODE> method of
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
177 the dialog returns three bits of information about the given item: its
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
178 type, its data handle and its rect (the on-screen <CODE>x,y,w,h</CODE>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
179 coordinates). We are only interested in the data handle here, on which
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
180 we call <CODE>SetDialogItemText()</CODE> to set our new text. Note
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
181 here that python programmers need not bother with the C-string versus
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
182 pascal-string controversy: the python glue module knows what is needed
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
183 and converts the python string to the correct type. <p>
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
184
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
185 And that concludes our first example of the use of resources and
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
186 dialogs. Next, you could have a look at the source of EasyDialogs for
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
187 some examples of using input fields and filterprocs. Or, go on with
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
188 reading the <A HREF="example2.html">second part</A> of this document
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
189 to see how to implement a better version of this application.<p>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
190
11518
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
191 </BODY>
cc412dfd79f7 New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
Jack Jansen <jack.jansen@cwi.nl>
parents: 7468
diff changeset
192 </HTML>
3457
a71d2fd90fa8 Explanations of the examples here
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
193