Linguistics 408/508 |
Fall 2003 |
Hammond |
Handout 19
Overview
- Questions from last time
- An example
- GUI programming
- Objects and
Tk
-
Tk
-object syntax
- Working with
Tk
An example
- Here is a little program
that is meant to exemplify a few things about
the final project.
- The program computes the log probability of a set of
monosyllabic nonsense forms with
respect to the newdic dictionary file. One way
to do this is to calculate the frequency of each
segment in the nonsense item and multiply these
together. Another way is to calculate the
frequency of the onset and the rhyme and then
multiply these together. Here is the result
of running the program.
- Notice that the program does something useful for
its author.
- Notice the approximate size of the program.
- Notice that there are lots of
comments, including a description of what the
program is supposed to do.
- Finally, Notice that I occasionally had to look
stuff up, that you will almost always have to
find ways to do things you haven't thought of
before.
GUI programming
- The basic idea is that you define some number of
graphical devices. These are then positioned in
a window. Your program waits for the user to
"do" something and then it responds.
- We've already done this in a limited way with
forms and CGI programs. You create your objects
with HTML forms. You then wait for the user to
press the "submit" button. That then causes
something else to happen via the CGI
program.
- This differs from traditional GUI programming two
ways:
- The only GUI event that the
program responds to immediately
is the submit button.
- The GUI elements are written in a
completely separate language
from the CGI program.
Objects and Tk
- We can think of GUI elements as "things" or
objects.
- These objects have to be created and positioned in
some window.
- In addition, we must define subroutines that take
place when these objects are interacted with in
various ways.
Tk
-object syntax overview
-
object->method()
. This runs a
subroutine that is specifically associated with
some GUIobject
-
GUIobject->otherGUIobject(-attribute
=> value)
. This creates a
GUIobject within some other object and sets some
property of that object.
-
GUIobject(-command => sub {
mysubroutine() }
. This sets the
subroutine that will run when a GUIobject is
selected.
-
MainWindow->new()
. This creates a
new main window.
Working with Tk
- All programs that make use of
Tk
have
this general framework.
First, we create a MainWindow
and
then we loop until the user interacts with a GUI
element.
- There are bijillions of GUI elements or
widgets. Here is a
Tk
program that exemplifies a couple of them (example). You
can see all of the Tk
widgets with the command
widget
.
- Properties. Each widget object is really a kind of
hash with a set of properties specific to each
widget type (each of which is marked with a
leading hyphen). One way to set those properties
is when the widget is created. We've already
seen a few properties:
-width
,
-height
, and -text
.
There are a number of others as well, e.g.
-background
(background color),
-foreground
(foreground color),
(text label). Here is a demo program for these:
example. (The
other way to set properties is with the
configure()
method, exemplified
below.)
- Two very useful properties are
-command
and
-textvariable
. The former allows
you to specify that some subroutine is to be
performed when a button is pressed. The latter
allows you to control the text that goes on a
widget (example).
-
pack()
. Finally, widgets are placed
in the window with the pack()
command. The position of widgets is controlled
by when they are packed, and by the
properties that are passed to the
pack()
command (example,
example, example).