Home > Software engineering >  Is there a way to use balloon on radiobutton in Perl Tk?
Is there a way to use balloon on radiobutton in Perl Tk?

Time:10-14

Is there a way to use balloon on radiobutton in Perl Tk? I want to display a balloonmsg and statusmsg when I hover over the buttons, if possible. I've googled but it doesn't seem like there is any documentation on this functionality. I wrote a simple code to portray my idea because I can't share the original code:

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;

And here is an image of the GUI:

RadioButton GUI

CodePudding user response:

The Tk::Balloon SYNOPSIS shows you can call Balloon on your main window and attach the balloon popup to each button, with the message showing up in your status bar:

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();

There is a custom message for each button.

CodePudding user response:

The answer posted by user toolic gave me an idea on how to get to what I want. I created hash variable that contains my custom messages. By passing in the key, I was able to have a unique message for each button. Here's what it looks like:

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();
  • Related