1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

[Chapter 17] 17.5 The URI Module

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.44 MB, 72 trang )


[Chapter 17] 17.5 The URI Module



q



as_string



q



base



q



crack



q



default_port



q



eparams



q



epath



q



eq



q



equery



q



frag



q



full_path



q



host



q



netloc



q



params



q



password



q



path



q



port



q



query



q



rel



q



scheme



q



strict



q



user



17.4 The HTML Module



VII. Perl/Tk



[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl

Programming | Perl Cookbook ]



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch17_05.htm (2 of 2) [2/7/2001 10:37:09 PM]



[Part VII] Perl/Tk



Part VII



Part VII: Perl/Tk

Chapter 18: Perl/Tk



17.5 The URI Module



18. Perl/Tk



[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl

Programming | Perl Cookbook ]



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/part07.htm [2/7/2001 10:37:11 PM]



[Chapter 18] Perl/Tk



Chapter 18



18. Perl/Tk

Contents:

Widgets

Geometry Managers

Common Widget Configuration Options

The Button Widget

The Checkbutton Widget

The Radiobutton Widget

The Label Widget

The Entry Widget

The Scrollbar Widget

The Listbox Widget

The Text Widget

The Canvas Widget

The Scale Widget

The Menubutton Widget

The Menu Widget

The Optionmenu Widget

The Frame Widget

The Toplevel Widget

Perl/Tk is an extension for writing Perl programs with a Graphical User Interface (GUI) on both Unix and

Windows 95/NT. Tk was originally developed as an extension to the Tcl language, for use with the X Window

System on Unix. With its port to Perl, Tk gives Perl programmers the same control over the graphical desktop that

Tcl programmers have taken for granted.

The Tk extension makes it easy to draw a window, put widgets into it (such as buttons, checkboxes, entry fields,

menus, etc.) and have them perform certain actions based on user input. A simple "Hello World" program would

look like this:

#!/usr/bin/perl -w

use Tk;

my $mw = MainWindow->new;

$mw->Button(-text => "Hello World!", -command =>sub{exit})->pack;

MainLoop;

When you run it, it would look like Figure 18.1.



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch18_01.htm (1 of 4) [2/7/2001 10:37:17 PM]



[Chapter 18] Perl/Tk



Figure 18.1: A simple Perl/Tk program



Pushing the "Hello World!" button exits the program, and your window disappears.

Let's walk through these few lines of code. After calling the Perl interpreter, the program calls the Tk module.

Then it proceeds to build a generic, standard window (MainWindow) to act as a parent for any other widgets you

create. Line 4 of the program creates a button and displays it using the pack geometry manager. It also gives the

button something to do when pushed (in this case, exit the program).

The very last line tells the program to "go do it." MainLoop starts the event handler for the graphical interface,

and the program draws any windows until it reaches the MainLoop statement. Everything up to that point is

preparation; until you reach the MainLoop statement, the program simply prepares its windows and defines what

to do when certain events happen (such as a mouse click on the "Hello World!" button). Nothing is drawn until

the MainLoop statement is reached.



18.1 Widgets

Widgets in Perl/Tk are created with widget creation commands, which include Button, Canvas,

CheckButton, Entry, Frame, Label, Listbox, Menu, Menubutton, Message, Radiobutton,

Scale, Scrollbar, Text, and Toplevel.

Positioning widgets is done with geometry managers. In the "Hello World" example shown earlier, the pack

command is the geometry manager. Geometry managers determine where in the window (or frame) the widget

will sit. We'll talk more about the Perl/Tk geometry managers later in this chapter.



18.1.1 Widget Methods

Widgets can be configured, queried, or manipulated via various widget methods. For example, all widgets support

the configure widget method for changing widget properties after the widget is created. In addition, most

widgets have specialized methods associated with them for manipulating the widget as needed throughout the

program. For example, widgets that scroll support the xview and yview methods for determining the viewable

portion of the content when the scrollbar is moved. The Entry and Text widgets have methods for inserting and

deleting values. The Canvas widget has a whole series of methods for drawing shapes and inserting text into the

canvas. And so on.

Widget methods are listed in the discussion of each widget later in this chapter. However, since all widgets

support the configure and cget methods, we're going to cover them now.

18.1.1.1 The configure method

The configure method can be used to set and retrieve widget configuration values. For example, to change the

width of a button:

$button->configure(-width => 100);

To get the value for a current widget, just supply it without a value:



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch18_01.htm (2 of 4) [2/7/2001 10:37:17 PM]



[Chapter 18] Perl/Tk



$button->configure(-width);

The result is an array of scalars; the values you care about are the last two, which represent the default value and

its current value, respectively.

You can also call configure without any options at all, which will give you a listing of all options and their

values.

18.1.1.2 The cget method

For simply retrieving the value of an option, configure returns more information than you generally want. The

cget method returns just the current value.



18.1.2 Scrollbars

Many widgets have scrollbars associated with them. Scrollbars can be added to a widget in two ways: either using

an independent Scrollbar widget or using the Scrolled method when creating a widget. For simple scrollbars,

the Scrolled method is much easier and therefore preferable.

18.1.2.1 Using the Scrolled method

You use the Scrolled method to create both the widget and the scrollbar in a single command. For example:

$mainwindow->Scrolled('Entry', -scrollbars => 'os'

-textvariable => \$address)->pack;

This creates an Entry widget with an "optional" scrollbar on the bottom. The first argument to Scrolled is the

type of widget (in this case, an Entry widget). Then use the -scrollbars option to list the location of the

scrollbar ("s" for the south, or bottom, edge of the widget). Here, we specify an "optional" scrollbar with "o",

meaning that the scrollbar will only appear if needed.

Any additional options to the Scrolled method are taken as options to the widget itself. In this case, we're

setting the -textvariable option to the Entry widget.

18.1.2.2 The Scrollbar widget

For more flexibility with a scrollbar, you can use the Scrollbar widget. To do so, you need to create the target

widget to scroll, set the -xscrollcommand or -yscrollcommand option as appropriate, configure the

scrollbar to talk to the widget, and position the scrollbar and target widget next to one another. For example:

$scrollbar = $mainwindow->Scrollbar(-orient => 'vertical');

$listbox = $mainwindow->Entry(-yscrollcommand => ['set' => $scrollbar]);

$scrollbar->configure(-command => ['yview' => $listbox]);

$scrollbar->pack(-side => 'right', -fill => 'y');

$listbox->pack(-side => 'left', -fill => 'both');

First, we create the scrollbar with vertical orientation (which is actually the default). Next, we create the Listbox

widget with the -yscrollcommand option to define a callback when the widget is scrolled vertically. The

scrollbar is then configured with a callback that says to inform the Listbox widget when it is clicked vertically.

Finally, the Scrollbar and Listbox widgets are packed side-by-side. See further discussion of the Scrollbar widget

later in this chapter for more information.



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch18_01.htm (3 of 4) [2/7/2001 10:37:17 PM]



[Chapter 18] Perl/Tk



18.1.3 Callbacks

Many widgets allow you to define a callback, which is a command to execute when the widget is selected. For

example, when you press an exit button, the callback might be to a routine that cleans up and quits the program.

When you click on a radio button, you might want to change the window to reflect the new preferences.

Widgets that support callbacks have a -command option to provide the callback function. In the "Hello World!"

example shown previously in this chapter, the callback is to sub {exit}. In that example, the callback is called

as an anonymous subroutine. You could also use a reference to a subroutine (e.g., \&routine). If you want to

provide arguments to a subroutine, you can call it as an anonymous list (e.g., [ \&routine, $arg, $arg,

... ]).



18.1.4 Colors and Fonts

Tk was originally created for the X Window System and is still primarily used in that environment. For that

reason, it has inherited the font and color scheme used for the X Window System.

Colors that can be used with Tk widgets are identified either by an RGB value or by a name that has been

associated with an RGB value. In general it is easiest to use a color name rather than an explicit RGB value; for a

listing of the color names that are supported, see the rgb.txt file in your X distribution or use the showrgb

command. (Most common color names are supported, so you can say things like "red," "pink," "green," and even

"chartreuse" with confidence.)

Fonts are another matter. Under the X Window System, fonts are named things like

-adobe-helvetica-medium-o-normal--12-120-75-75-p-67-iso8859-1. Wildcards can make the fonts easier to use,

but they're still a mouthful. For a listing of fonts available for a particular X server, use the xlsfonts command.

There are a few font "aliases" that have been defined for your convenience (such as fixed, 6x10, 9x15, etc.),

and you might prefer to just stick to those.



VII. Perl/Tk



18.2 Geometry Managers



[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl Programming | Perl

Cookbook ]



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch18_01.htm (4 of 4) [2/7/2001 10:37:17 PM]



[Chapter 18] 18.2 Geometry Managers



Chapter 18

Perl/Tk



18.2 Geometry Managers

Creating widgets and determining how to display them are done with separate commands. You can create a

widget with one of the widget creation methods (such as Button, Canvas, etc.), but you display them

using a geometry manager. The three geometry managers are pack, grid, and place. pack is by far the

most commonly used.

You can either pack a widget as you create it, or you can create the widget object and pack it separately. For

example, the previous "Hello World!" example might have read:

#!/usr/bin/perl -w

use Tk;

my $mw = MainWindow->new;

$button = $mw->Button(-text => "Hello World!", -command =>sub{exit});

$button->pack;

MainLoop;



18.2.1 The pack Geometry Manager

With the pack geometry manager, widgets cannot overlap or cover each other, either partially or completely.

Once a widget is packed into a window, the next widget is packed in the remaining space around it. pack

sets up an "allocation rectangle" for each widget, determined by the dimensions of the parent window and the

positioning of the widgets already packed into it. This means that the order in which you pack your widgets is

very important.

By default, pack places widgets at the top center of the allocation rectangle. However, you can use options

to pack to control where a widget is placed and how much padding is placed around it. Options for pack

are:

-side => side

Puts the widget against the specified side of the window. Values for side are 'left', 'right',

'top', and 'bottom'. The default is 'top'.

-fill => direction

Causes the widget to fill the allocation rectangle in the specified direction. Values for direction are

'none', 'x', 'y', and 'both'. The default is 'none'.

-expand => boolean



http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch18_02.htm (1 of 6) [2/7/2001 10:37:22 PM]



[Chapter 18] 18.2 Geometry Managers



Causes the allocation rectangle to fill the remaining space available in the window. Values are 'yes',

'no', 1, and 0. The default is 0 ('no').

-anchor => position

Anchors the widget inside the allocation rectangle. Values for position are 'n', 'ne', 'e',

'se', 's', 'sw', 'w', 'nw', and 'center'. The default is 'center'.

-after => $widget

Puts the widget after another widget in packing order.

-before => $widget

Puts the widget before another widget in packing order.

-in => $window

Packs the widget inside another window rather than inside its parent.

-ipadx => amount

Increases the size of the widget horizontally by amount * 2. amount can be represented as a number

followed by c (centimeters), i (inches), m (millimeters), and p (printer points). Pixels are the default

units.

-ipady => amount

Increases the size of the widget vertically by amount * 2. amount can be represented as a number

followed by c (centimeters), i (inches), m (millimeters), and p (printer points). Pixels are the default

units.

-padx => amount

Places padding on the left and right of the widget. amount can be represented as a number followed

by c (centimeters), i (inches), m (millimeters), and p (printer points). Pixels are the default units.

-pady amount

Places padding on the top and bottom of the widget. amount can be represented as a number followed

by c (centimeters), i (inches), m (millimeters), and p (printer points). Pixels are the default units.

18.2.1.1 Pack methods

The following methods are associated with pack:

packForget

Causes a widget to be removed from view.

$widget->packForget;

The widget is not destroyed, but is no longer managed by pack. The widget is removed from the

packing order, so if it were repacked later, it would appear at the end of the packing order.

packInfo

Returns a list containing all pack information about that widget.

$info = $widget->packInfo;

packPropagate

http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch18_02.htm (2 of 6) [2/7/2001 10:37:22 PM]



Xem Thêm
Tải bản đầy đủ (.pdf) (72 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×