Perl/Tk 菜单栏怪癖

发布于 2024-11-06 07:14:42 字数 1769 浏览 0 评论 0原文

我正在尝试添加一个带有标准文件打开、保存和新建选项的菜单栏。 然而,处理打开、保存和新操作的子例程并没有按预期运行,而是在创建框架时启动。但是,当我真正点击它们时,事实并非如此。

以下是我正在使用的代码。 (主窗口仅包含菜单栏)

#!/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";
    }
}

I'm trying to add a menubar with the standard File Open, Save and New options.
However, instead of behaving as expected, the subroutine handling the open, save and new actions is launched upon creation of the frame. But, when I actually click on them, it is not.

Following is the code I'm using. (Main window contains only the menubar)

#!/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";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-11-13 07:14:42

这是因为 &menu_file('n')调用子例程的语法(更多详细信息)。相反,你必须这样做:

$mw->bind('<Control-n>' => sub{menu_file('n')});

或者像这样:

$mw->bind('<Control-n>' => [\&menu_file, 'n']);

That's because &menu_file('n') is syntax for invoking a subroutine (more details). Instead, you have to do it like this:

$mw->bind('<Control-n>' => sub{menu_file('n')});

Or like this:

$mw->bind('<Control-n>' => [\&menu_file, 'n']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文