Perl Tkxを使用して、私はユーザから何らかの入力を得て、ウィンドウを閉じて、後でもう一度やり直したいと思っています。ユーザーの入力のために、私はちょうどいくつかのボタンを表示しています、そして、ユーザーはそれらの1つをクリックします。ここで私は今持っているものだ:、どのように私は繰り返しTkxでユーザを促すことができますか?
sub prompt_user {
my $answer;
my $mw = Tkx::widget->new("."); ## the main window is unavailable the second time
$mw->g_wm_title("Window Title"); ## line 40
$mw->g_wm_minsize(300, 200);
my $label = $mw->new_label(-text => "Question to the user?");
$label->g_pack(-padx => 10, -pady => 10);
my $button1 = $mw->new_button(
-text => "Option One",
-command => sub { $answer = 0; $mw->g_destroy; },
);
$button1->g_pack(-padx => 10, -pady => 10);
my $button2 = $mw->new_button(
-text => "Option Two",
-command => sub { $answer = 1; $mw->g_destroy; },
);
$button2->g_pack(-padx => 10, -pady => 10);
Tkx::MainLoop(); ## This blocks until the main window is killed
return $answer;
}
したがって、ユーザーがいずれかのボタンをクリックすると、ウィンドウが閉じprompt_user()(ユーザーがクリックによってどのボタンで)0または1を返し、実行が継続されます。私は再びユーザーにプロンプトを表示しようとするまで。その後、私はエラーを取得:
can't invoke "wm" command: application has been destroyed at MyFile.pm line 40
私はちょうどたいユーザーがクリックされた1を参照するのを待つ、いずれかをクリックしてみましょう、ボタンの束を設置する方法を、多分後でもう一度それを行います。メインウィンドウを破壊することなくボタンクリックに対する応答を待つ方法はありますか?おそらくサブウィンドウを作りますか?
私はTkxを使い始めたばかりで、グーグルでは上記のコード(MainLoop/g_destroyを使用)のような簡単な例がたくさんありますが、ウィンドウを再作成する例は見つかりませんでした。私はダイアログボックスやメッセージボックスについての情報を見たことがありますが、それらは私のニーズに合っていません。私はボタンにテキストを置き、任意の数のボタンを使用したい(私はyes/no/cancelに制限されたくないので、3つのオプションしか持たない)。
更新 は、ここで私は
# hide the main window, since I'm not using it
my $mw = Tkx::widget->new(".");
$mw->g_wm_withdraw();
# function to prompt the user to answer a question
# displays an arbitrary number of answers, each on its own button
sub prompt {
my $prompt = shift;
my $list_of_answers = shift;
# Note: the window name doesn't matter, as long as it's './something'
my $answer = Tkx::tk___dialog("./mywindowpath", # window name
"Prompt", # window title
$prompt, # text to display
undef, # tk bmp library icon
undef, # default button
@$list_of_answers); # list of strings to use as buttons
return $answer;
}
# use the button to ask a question
my $index = prompt("Who was the best captain?",
[ "Kirk", "Picard", "Cisco", "Janeway", "Archer" ]);
より良い応答を得るためにtkタグが追加されました。 – slebetman
時にはGUI要素をポップアップするコマンドラインプログラム(GUIなし)を作成しようとしていますか?それは可能だが、扱いにくい。 –
@Michael - はい、まさに私がやろうとしていることです。はい、それはするのが厄介なようです。 – Tim