Perl tk manual




















It shows you two widget types, a Label and a Button , and how they are packed. When clicked, the Button widget invokes the callback specified by the -command option.

Tk windows and widgets are hierarchical, i. Keep track of the main handle, commonly called a widget reference. You can use any Tk handle to create child widgets within the window or widget.

This is done by calling the Tk constructor method on the variable. In the constructor call, you can specify various options; you can later add or change options for any widget using the configure method, which takes the same parameters as the constructor. The one exception to the hierarchical structure is the Toplevel constructor, which creates a new outermost window.

After you create any widget other than the MainWindow or Toplevel s, you must render it by calling pack. This is not entirely true; more later. If you do not need to refer to the widget after construction and packing, call pack off the constructor results, as shown for the Label and Button in the example above.

Note that the result of the compound call is the result of pack , which is a valid Tk handle. Windows and widgets are deleted by calling destroy on them; this will delete and un-draw the widget and all its children. Most graphical interfaces are used to set up a set of values and conditions, and then perform the appropriate action. The Tk toolkit is different from your average text-based prompting or menu driven system in that you do not collect settings yourself, and decide on an action based on an input code; instead, you leave these values to your toolkit and only get them when the action is performed.

The slightly larger example below shows how to do this in Tk. Note the use of callbacks. Note, also, that Tk handles the values, and the subroutine uses the method get to get at the values.

If a user changes his mind and wants to change the font again, the application never notices; it's all handled by Tk. In the examples above, you must have noticed the pack calls. This is one of the more complicated parts of Tk.

The basic idea is that any window or widget should be subject to a Tk geometry manager; the packer is one of the placement managers, and grid is another. The actions of the packer are rather simple: when applied to a widget, the packer positions that widget on the indicated position within the remaining space in its parent. By default, the position is on top; this means the next items will be put below. You can also specify the left, right, or bottom positions. Additional packing parameters specify the behavior of the widget when there is some space left in the Frame or when the window size is increased.

If widgets should maintain a fixed size, specify nothing; this is the default. These parameters are not shown in the example below; see the widget demonstration. If you want to group some items within a window that have a different packing order than others, you can include them in a Frame.

This is a do-nothing window type that is meant for packing or filling and to play games with borders and colors. Most real applications require more than one window. As you just saw, you can create more outermost windows by using a Toplevel widget. Each window is independent; destroying a Toplevel window does not affect the others as long as they are not a child of the closed Toplevel. However, exiting the MainWindow will destroy all remaining Toplevel widgets and end the application.

The example below shows a trivial three-window application:. So far, all callback routines shown called a user procedure. You can also have a callback routine call another Tk routine. This is the way that scroll bars are implemented: scroll-bars can call a Tk item or a user procedure, whenever their position has changed. The Tk item that has a scrollbar attached calls the scrollbar when its size or offset has changed.

In this way, the items are linked. You can still ask a scrollbar's position, or set it by hand - but the defaults will be taken care of. The example below shows a Listbox with a scroll bar. Moving the scrollbar moves the Listbox. Scanning a Listbox dragging an item with the left mouse button moves the scrollbar. Note that there's a convenience method Scrolled which helps constructing widgets with automatically managed scrollbars. One of the most powerful widgets in Tk is the Canvas window.

In a Canvas window, you can draw simple graphics and include other widgets. The Canvas area may be larger than the visible window, and may then be scrolled. Any item you draw on the canvas has its own id, and may optionally have one or more tags. You may refer to any item by its id, and may refer to any group of items by a common tag; you can move, delete, or change groups of items using these tags, and you can bind actions to tags.

Perl was originally a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It quickly became a good language for many system management tasks. Over the years, Perl has grown into a general-purpose programming language. It's widely used for everything from quick "one-liners" to full-scale application development.

The language is intended to be practical easy to use, efficient, complete rather than beautiful tiny, elegant, minimal. It combines in the author's opinion, anyway some of the best features of sed , awk , and sh , making it familiar and easy to use for Unix users to whip up quick solutions to annoying problems.

Its general-purpose programming facilities support procedural, functional, and object-oriented programming paradigms, making Perl a comfortable language for the long haul on major projects, whatever your bent. Perl's roots in text processing haven't been forgotten over the years. It still boasts some of the most powerful regular expressions to be found anywhere, and its support for Unicode text is world-class. It handles all kinds of structured text, too, through an extensive collection of extensions.



0コメント

  • 1000 / 1000