原文:
10.4. How do I get a Popup to popup?
For things like a simple "are you sure?" dialog box you might want to take a look at Dialog.pm which is discussed in a later question within this FAQ [16.1].
If you don't wish to require Tk::Dialog, you need something more complicated, or you simply want to create your own independent window with widgets; you must first setup a Toplevel in perl/Tk. The fourth example in UserGuide.pod gives a simple example of how to call Toplevel. Quoting from that script: my $main = new MainWindow; fill_window($main, 'Main'); my $top1 = $main->Toplevel;
Where sub fill_window is declared after the call to MainLoop;. When running that script take careful note of which window pops up first, which window has grabbed the active attention of your input device(s), and which widget within the active window has the keyboard/mouse focus when all three windows are open.
The use of Toplevels brings up the issue of grab - or which independent window is presently "active" and which are activatable. To make a Toplevel window active call grab thusly: $Top_widget->grab(grab_option);
where $Top_widget identifies the desired Toplevel (it would be either $top1 or $top2 in the sample script referred to above). grab_option could be -global - but this is discouraged as a sign of "desparate programming style". To give a Toplevel "local grab" you may simply say: $Top_widget->grab;
That is, without an argument.
The use of Toplevels may also bring up the issue of focus - or which window - even which widget within a window - is presently "hot". You may call focus on an entire Toplevel: $Top_widget->focus;
However, focus is most often used with individual widgets rather than a whole Toplevel.
To de-iconify a widget there is in fact a Popup function that may be called thusly: $Top_widget->Popup();
译文:
10.4. 如何设置一个弹出窗口?
对于只是一个简单的确认信息的对话框,比如“确定吗?”,你可以参考Dialog.pm的内容,具体细节我们将在后面的问题中讨论。
如果你不想使用Tk::Dialog模块,或者你需要更复杂一些的功能,或者你就是想用基本组件独立构建一个自己的窗口,那么你必须首先创建一个 Perl/Tk中的Toplevel组件。UserGuide.pod中的第四个例子就给出了如何调用Toplevel的一个简单的例子(译者注:在命令行中使用perldoc Tk::UserGuide就可以看到了!)。下面就是那个脚本中语句:
my $main = new MainWindow;
fill_window($main, 'Main');
my $top1 = $main->Toplevel;
这里的fill_window子程序是在MainLoop语句之后定义的。当运行这个脚本的时候,请特别注意这里是哪一个窗口先弹出的?哪一个窗口当前处于输入设备的热点(即当前窗口)?以及在三个窗口都打开后,键盘和鼠标的焦点在当前窗口中的哪一个组件中?
使用Toplevel后就必然引入了要使用grab的问题,也就是说在那些独立的窗口中,哪一个是当前的活动窗口?以及那些是可以激活的?要激活一个Toplevel的窗口,可以使用如下的grab语句:
$Top_widget -> grap(grab_option);
这里$Top_widget指要被激活的Toplevel窗口(可以是上面例子中的$top1或$top2)。grap_option可以是global,但是从“分布编程风格”(??)的角度出发,我们并不鼓励这样使用。要设定一个Toplevel为“局部热点”,可以使用下面的方法:
$Top_widget -> grab;
也就是说,不带任何参数。
使用Toplevel同样会引入关于focus的问题,也就是说哪一个窗口,甚至是窗口中的哪一个组件是当前的“热点”。可以对整个Toplevel调用用focus方法:
$Top_widget -> focus;
但是,实际上focus更多的是被用在单个组件上而不是整个Toplevel上。
要还原一个已经最小化的窗口组件,实际上可以如下的使用Popup方法:
$Top_widget -> Popup();