0
我試圖添加標準文件打開,保存和新選項的菜單欄。 然而,處理打開,保存和新操作的子例程不是按照預期運行,而是在創建框架時啓動。但是,當我實際點擊它們時,事實並非如此。Perl/T菜單欄怪癖
以下是我正在使用的代碼。 (主窗口只包含菜單欄)
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
use Tk 8.0;
use Tk::NoteBook;
use Tk::MsgBox;
my $mw=MainWindow->new;
$mw->geometry("+500+300");
# Menu Bar Buttons
my $mbar=$mw->Menu();
$mw->configure(-menu => $mbar);
my $file=$mbar->cascade(-label=>"~File", -tearoff => 0);
my $help=$mbar->cascade(-label =>"~Help", -tearoff => 0);
# File Menu
$file->command(-label =>'~New ', -command=>&menu_file('n'), -accelerator=>'Ctrl+N');
$file->command(-label =>'~Open ', -command=>&menu_file('o'), -accelerator=>'Ctrl+O');
$file->command(-label =>'~Save ', -command=>&menu_file('s'), -accelerator=>'Ctrl+S');
$file->separator();
$file->command(-label =>'~Quit ', -command=>sub{exit}, -accelerator=>'Ctrl+Q');
# Help Menu
$help->command(-label => 'Version');
$help->separator;
$help->command(-label => 'About');
# Menu Bar Accelerators
$mw->bind('<Control-n>', &menu_file('n'));
$mw->bind('<Control-o>', &menu_file('o'));
$mw->bind('<Control-s>', &menu_file('s'));
$mw->bind('<Control-q>', sub{exit});
MainLoop;
sub menu_file {
my $opt=shift;
my $filetypes = [
['Codac files', '.k'],
['All Files', '*' ],
];
if($opt eq 's'){
my $txt_ent_script = $mw->getSaveFile(-filetypes=>$filetypes, -initialfile=>'jitter', -defaultextension=>'.k');
print "Output filename: $txt_ent_script\n";
}
}
謝謝,像宣傳的那樣:) – kshenoy 2011-05-10 13:17:44
注意這兩種形式是微妙的不同。請參閱[對相關問題的回答](http://stackoverflow.com/questions/2620461#2622318)以獲取解釋。 – 2011-05-10 14:18:48