|
|
|
|
|
and so on. The most common thing to do after creating a listbox is to use the insert method to insert items into it: |
|
|
|
 |
|
|
|
|
$lb->insert('end', @listbox_items);
# or...
$lb->insert('end', $item1, $item2, $item3); |
|
|
|
|
|
|
|
|
The insert method takes an index value as the first argument; the rest of the arguments will be considered items to be put into the listbox. Listbox indexes are similar to the entry widget indexes except they refer to lines instead of individual characters. |
|
|
|
|
|
|
|
|
We could use a listbox instead of radiobuttons to select our window background color (see Chapter 4, Checkbuttons and Radiobuttons, for the radiobutton example). The listbox code looks like this: |
|
|
|
 |
|
|
|
|
$lb = $mw->Listbox(-selectmode => "single")->pack();
$lb->insert('end', qw/red yellow green blue grey/);
$lb->bind('<Button-1>',
sub { $lb->configure(-background =>
$lb->get($lb->curselection() ) );
}); |
|
|
|
|
|
|
|
|
The -selectmode option limits the number of selections to one. We insert some colors to choose from. There is no -command option for a listbox, so we use bind (see Chapter 14, Binding Events) to have something happen when the user clicks on an item with the left mouse button. Using the listbox methods get and curselection, we determine which item the user clicked on and then set the background of the listbox to that color. There are only five colors in our example here; you can use more colors and add a scrollbar to make it more useful. You can add a scrollbar by changing the line with Listbox in it: |
|
|
|
 |
|
|
|
|
$lb = $mw->Scrolled("Listbox", -scrollbars => "e",
-selectmode => "single")->pack(); |
|
|
|
|
|
|
|
|
All the other lines in the program remain unchanged. For more information about adding and utilizing scrollbars, see Chapter 6, Scrollbars. Now that we've looked at an example, let's go over the options and methods that let us use the listbox the way we want to. |
|
|
|
|
|
|
|
|
As with any of the widgets, you can configure the listbox using options. The standard widget options are -cursor, -font, -height, -highlightbackground, -highlightcolor, -highlightthickness, -takefocus, -width, -xscroll-command, and -yscrollcommand. The options that behave the same for each widget will only be listed in the following list. Those options specific to listbox widgets will be discussed later in this chapter. |
|
|
|
|