原文:
16.1. How do I get a Dialog box?
For things like a simple "are you sure?" dialog box you might want to take a look at perl5/Tk/Dialog.pm. This module may be invoked with require Tk::Dialog; etc. - there are much more extensive directions inside the comment fields at the top of the Dialog.pm file itself. The module has a lot of options and has a tutorial driver script in perl5/Tk/demos/dialog. Dialog.pm is also used by the perl5/Tk/demos/widget demo. In particular look at perl5/Tk/demos/widget_lib/dialog1.pl and dialog2.pl for examples of how one makes use of Tk::Dialog. A snippet of a script that uses this module could look like: require Tk::Dialog; my $mw = MainWindow->new; my $D = $mw->Dialog( -title => 'Are you sure?', -text => "You have requested rm \*\nAre you sure?", -default_button => 'No', -buttons => ['No','yes'] ); my $choice = $D->Show; # use Show for Tk-b9.01# if using Tk-b8: my $choice = $D->show; print " you chose $choice \n";
A question concerning configuration of the Subwidgets on the Dialogs came up recently: <Greg_Cockerham@avanticorp.com> wrote:! I want to reconfigure the colors of the Dialog and! ErrorDialog buttons. How do I do this?! Thanks in advance. $dialog_widget->configure(-background => 'purple');
Since these two widgets are composites you manage them like any 'ol widget. If the default delegate subwidget(s) aren't to your liking you can always get to individual component widgets of the composite via the ->Subwidget() method.
I see these subwidgets:
Dialog 'message' is the label subwidget with the dialog text, and 'bitmap' is the label subwidget showing the dialog bitmap ErrorDialog 'error_dialog' is Dialog subwidget, 'text' is text subwidget
You can even do things like this:
$error_dialog->Subwidget('error_dialog')-> Subwidget('bitmap')->configure(..);
to "get to" the label widget of the dialog component of the error_dialog widget.....
Be sure to also check out the "dialog" demo.
译文:
16.1 如何使用对话框?
对于只显示诸如“确定吗?”这样的简单信息的对话框窗口,你也许应该看一看perl5/Tk/Dialog.pm模块(译者注:可以在命令行中使用 perldoc Tk::Dialog来直接参看手册)。这个模块可以通过使用require Tk::Dialog;等方法引入,更多详细的介绍可以在Dialog.pm文件本身前部的注释区中找到。另外,这个模块还有很多选项,并且在 perl5/Tk/demos/dialog中还有一些演示脚本。同时,Dialog.pm也有相应当例子在widget演示程序中。特别是,可以通过 perl5/Tk/demos/widget_lib/dialog1.pl和dialog2.pl两个脚本,学习一下如何实际的使用Tk:: Dialog。下面是一个简单的使用这个模块的例子:
require Tk::Dialog;
my $mw = MainWindow->new;
my $D = $mw->Dialog(
-title => 'Are you sure?',
-text => "You have requested rm \*\nAre you sure?",
-default_button => 'No',
-buttons => ['No','yes']
);
my $choice = $D->Show;
print " you chose $choice \n";
最近有人提出了一个关于配置对话框的“子组件”(Subwidgets)的问题:
<Greg_Cockerham@avanticorp.com> 写到:
!我想重新配置对话框的颜色和错误信息框(ErrorDialog)的按钮。
!我应该如何如何实现?谢谢先。
$dialog_widget->configure(-background => 'purple');
虽然这两个组件都是复合组件,但是你也可以像对简单组件那样操作它们的。即使默认的代表子组件不是你所想要的,你也可以通过->Subwidget()方法来控制复合组件中的每一个元件。
对于这两个复合组件,我们可以看到如下的子组件:
Dialog
‘message’是标签子组件,用来显示对话框的文字;而’bitmap’也是一个标签子组件用来显示对话框的位图。
ErrorDialog
‘error_dialog’是一个对话框子组件;而’text’是一个文本子组件。
你甚至可以用下面的方法来配置error_dialog组件中对话框元件的标签组件:
$error_dialog->Subwidget('error_dialog')->
Subwidget('bitmap')->configure(..);
最后,请务必仔细参阅dialog的演示。