在Perl Tk中有没有一种方法可以在单选按钮上使用气球?

tzxcd3kk  于 2022-11-15  发布在  Perl
关注(0)|答案(2)|浏览(156)

在Perl Tk中有没有一种方法可以在单选按钮上使用balloonmsg?如果可能的话,我想在我悬停在按钮上时显示balloonmsg和statusmsg。我在谷歌上搜索过,但似乎没有任何关于此功能的文档。我写了一个简单的代码来描述我的想法,因为我不能分享原始代码:

use Tk;
use Tk::Balloon;

$window = new MainWindow;
$window -> title("Direction");

$window_frame = $window -> Frame();
$window_frame -> pack(-anchor => 'w', -fill => 'both');

$status_bar = $window_frame -> Label(-relief => 'groove') -> pack(-side => 'bottom', -fill => 'x');

$direction = 'Left';
foreach('Left', 'Right', 'Up', 'Down') {
    $direction_button = $window_frame -> Radiobutton(-text => $_, -variable => \$direction, -value => $_) 
                                        -> pack(-side => 'left', -anchor => 'center',  -padx => '15');
}

MainLoop;

下面是GUI的图像:

kuhbmx9i

kuhbmx9i1#

Tk::Balloon SYNOPSIS显示您可以在主窗口中调用Balloon,并在每个按钮上调用attach气球弹出窗口,同时在状态栏中显示以下消息:

use warnings;
use strict;
use Tk;
use Tk::Balloon;

my $window = new MainWindow;
$window->title("Direction");

my $window_frame = $window -> Frame();
$window_frame -> pack(-anchor => 'w', -fill => 'both');

my $status_bar = $window_frame -> Label(-relief => 'groove')
    ->pack(-side => 'bottom', -fill => 'x');

my $direction = 'Left';
foreach ('Left', 'Right', 'Up', 'Down') {
    my $direction_button = $window_frame->
        Radiobutton(-text => $_, -variable => \$direction, -value => $_) 
        ->pack(-side => 'left', -anchor => 'center', -padx => '15');
    my $balloon = $window->Balloon(-statusbar => $status_bar);
    $balloon->attach($direction_button, -balloonmsg => "Go $_",  
        -statusmsg => "Press the Button to go $_");
}
MainLoop();

每个按钮都有一条自定义消息。

w8f9ii69

w8f9ii692#

用户toolic发布的答案让我知道了如何获得我想要的信息。我创建了一个包含自定义消息的哈希变量。通过传入键,我可以为每个按钮提供一个唯一的消息。如下所示:

use warnings;
use strict;
use Tk;
use Tk::Balloon;

my $window = new MainWindow;
$window->title("Direction");

my $window_frame = $window -> Frame();
$window_frame -> pack(-anchor => 'w', -fill => 'both');

my $status_bar = $window_frame -> Label(-relief => 'groove')
    ->pack(-side => 'bottom', -fill => 'x');

my %message = ('Left', 'Go West!', 'Right', 'Go East!', 'Up', 'Go North', 'Down', 'Go South');

my $direction = 'Left';
foreach ('Left', 'Right', 'Up', 'Down') {
    my $direction_button = $window_frame->
        Radiobutton(-text => $_, -variable => \$direction, -value => $_) 
        ->pack(-side => 'left', -anchor => 'center',  -padx => '15');
    my $balloon = $window->Balloon(-statusbar => $status_bar);
    $balloon->attach($direction_button, -balloonmsg => "$message{$_}",  
        -statusmsg => "Press the Button to $message{$_}");
}
MainLoop();

相关问题