[ Pobierz całość w formacie PDF ]
.We'll use this method to dynamically create menus in Chapter 16.14.3.9 Scrollbars and ScrollingAlthough scrollbars are full-fledged widgets, they are seldom used by themselves; they always controlassociated widgets.Because of this close association, Perl/Tk provides a convenience function called Scrolledthat wraps scrollbars around a widget of your choice without having to explicitly create, size, and pack them.The following example creates a scrolled listbox:$scrolled_list = $top->Scrolled('Listbox', listbox options,scrollbars => 'se');Internally, this creates a Frame widget, horizontal and vertical scrollbars (if required), and a listbox; packs all ofthem together; and returns the reference to the Frame widget (the container).Isn't that nice? In fact, for the mostcommon case of scrolled listboxes and scrolled text boxes, Perl/Tk provides convenient methods calledScrlListBox and ScrlText, respectively, reducing your typing even further:$scrolled_list = $top->ScrlListBox(listbox options);This is typically all you need to know about scrolling, and you can safely go on to the Section 14.3.10, "Scale"section without loss of continuity.14.3.9.1 Custom scrollingThere are times, though, when you want to handle your own scrolling.For example, suppose you have threelistboxes and want to synchronize their scrolling.This means that you need to arrange to have the scrollbar sendmessages to all three widgets whenever its slider is moved.The subtle issue here is that the inverse also holdstrue: the widgets also should send messages when they are themselves scrolled by using other means.Forexample, if you click on one listbox and drag the cursor, the listbox will scroll its own contents.It must thenmake sure that the scrollbar and the other two listboxes are in sync.In other words, the scrollbar is not always inthe driver's seat; it is very much a "I'll scroll you, you scroll me" kind of relationship.As Table A.11 shows, there is no explicit property tying a scrollbar to a widget, but the scrollbar does have acallback property called command that is notified when the slider is moved.Meanwhile, it so happens that allwidgets that are scrollable (listboxes, text widgets, frames, and canvases) support two methods called xview andyview (Table A.12), which tell the scrollable widget what part of its contents to show in its window.Hence, tomake a scrollbar send a message to a widget to scroll itself, we configure the scrollbar's command property likethis:$scrollbar->configure (command => [N$widget]);The scrollbar automatically calls the specified method (xview or yview) on the widget.How does the widgetknow where to scroll to? Ah, unbeknownst to you, the scrollbar supplies some arguments to the yviewinvocation, so internally, the message from the scrollbar to the widget might look like this:$widget->yview('moveto', 30);This tells the widget to align its contents such that the top line or pixel represents the 30% mark.Now let us look in the other direction, where the widget informs the scrollbar.All scrollable widgets support two methods called xscrollcommand and yscrollcommand, which should be setup to call the scrollbar's set method as follows:$listbox->configure ('yscrollcommand', [N$scrollbar]);Figure 14.7 shows this symbiotic relationship.The details of the commands and properties described above areprovided in Tables Table A.11 and Table A.12.Figure 14.7: Interaction between a scrollbar and an associated widget (a listbox)Note that in this example, you don't have to make each listbox drive the other two listboxes.It is enough if eachof them drives the scrollbar, because the scrollbar is tied to the other two.Example 14.5 puts the various configuration commands together for one list.Example 14.5: Setting up a Scrollbar and Listbox to Scroll Each Otheruse Tk;$top = MainWindow->new();$car_list = $top->Listbox("width" => 15, "height" => 4,)->pack(side => 'left',padx => 10);$car_list->insert('end', # Insert at end, the following list"Acura", "BMW", "Ferrari", "Lotus", "Maserati","Lamborghini", "Chevrolet");# Create scrollbar, and inform it about the listbox$scroll = $top->Scrollbar(orient => 'vertical',width => 10,command => ['yview', $car_list])->pack(side => 'left',fill => 'y',padx => 10);# Inform listbox about the scrollbar$car_list->configure(yscrollcommand => ['set', $scroll]);MainLoop();14.3.10 ScaleThe Scale widget is like a thermometer.It displays tick marks along a horizontal or vertical "trough" andprovides a slider inside the trough that can be moved programmatically or manually (with the mouse orkeyboard).Table A.13 shows the scale's properties and methods.Figure 14.8 shows two scales displaying Celsius and Fahrenheit values (corresponding to 0-100 degreesCelsius).The scales are coordinated so that a movement of one slider causes a corresponding movement in theother.Figure 14.8: Coordinated Celsuis and Fahrenheit scalesExample 14
[ Pobierz całość w formacie PDF ]