有沒有辦法在 Perl Tk 的單選按鈕上使用氣球?如果可能,當我將滑鼠懸停在按鈕上時,我想顯示一個 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 的影像:

uj5u.com熱心網友回復:
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();
每個按鈕都有一條自定義訊息。
uj5u.com熱心網友回復:
用戶發布的答案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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516899.html
標籤:perlperltk
上一篇:Perl搜索字串并列印出一行
下一篇:找到特定字符后洗掉字串
