Re: How to exit out of program when "X" on listbox is clicked



Jack:

Here is the code upto the first listbox. I need to have it such that if
the user clicks ok without selecting a listbox item, the program exits.
I also need the program to exit if the user clicks the cancel(X)
button:

#! /usr/bin/perl -w
require 5.003 ;
use Tk;
my @globalListContents = ( 'zero' , 'one' , 'two' , 'three' , 'four' )
;
print @globalListContents;
############################################################
# MAIN ROUTINE#

$title = "Listbox example";
$mw = new MainWindow;
$mw->title ( $title ) ;
$mw->Label ( -text => $title ) ->pack ( -side => 'top' , -anchor =>
'center' ) ;
# CREATE LISTBOX#
$list = $mw->Scrolled ( 'Listbox' , -scrollbars => 'osoe' ) ;
$list->configure ( -height => 8 , -width => 40 ) ;
$list->configure ( -selectmode => "browse" ) ;
$list->pack();

$but = $mw -> Button(-text => "OK",
-command =>\&push_buttonv);
$but -> pack();

########################################
# POPULATE LISTBOX#
$list->insert ( 0 , @globalListContents ) ;

########################################
# SHOW THE WINDOW#
$list->focus();
MainLoop();

sub push_buttonv {
$v_field = $list->get($list->curselection);
print $v_field;
$mw -> destroy;
#need to exit if user does not select
if (!$v_field) {
exit 0;
}
}

#################################################
So far, I get this error if I click ok without selection:


Tk::Error: wrong # args: should be ".frame.listbox get first
?last?" at D:/Perl/site/lib/Tk.pm line 228.
Tk callback for .frame.listbox
Tk::__ANON__ at D:/Perl/site/lib/Tk.pm line 228
Tk::Derived::Delegate at D:/Perl/site/lib/Tk/Derived.pm line 467
Tk::Widget::__ANON__ at D:/Perl/site/lib/Tk/Widget.pm line 313
main::push_buttonv at perlex.pl line 47
[\&main::push_buttonv]
Tk callback for .button
Tk::__ANON__ at D:/Perl/site/lib/Tk.pm line 228
Tk::Button::butUp at D:/Perl/site/lib/Tk/Button.pm line 111
(command bound to event)

Any suggestions?

TIA

Tony

.