简体   繁体   English

如何在Perl / Tk中关闭窗口?

[英]How can I close a window in Perl/Tk?

In my Perl/Tk script I have opened two windows. 在我的Perl / Tk脚本中,我打开了两个窗口。 After a specific button click I want to close one of them. 单击特定按钮后,我想关闭其中一个。 How can I do that? 我怎样才能做到这一点? Here's what I have so far: 这是我到目前为止所拥有的:

$main = new MainWindow;
$sidebar = $main->Frame(-relief => "raised", 
                        -borderwidth => 2)
                ->pack (-side=>"left" ,
                        -anchor => "nw", 
                        -fill   => "y");
$Button1 = $sidebar -> Button (-text=>"Open\nNetlist", 
                               -command=>  \&GUI_OPEN_NETLIST) 
                    ->pack(-fill=>"x");
MainLoop;

sub GUI_OPEN_NETLIST
{
    $component_dialog = new MainWindow;
    $Button = $component_dialog -> Button (-text=>"Open\nNetlist", 
                                           -command=>  **close new window**) 
                                ->pack(-fill=>"x"); 
    MainLoop;
}

The simplist way is to call $component_dialog->destroy in the buttons -command callback. 简单的方法是在按钮 - -command回调中调用$component_dialog->destroy This has the disadvantage that if you want to redisplay the window later you have to recreate it. 这样做的缺点是,如果您想稍后重新显示窗口,则必须重新创建它。 The withdraw method will hide the window without destroying it so you can redisplay it later if you need to. withdraw方法将隐藏窗口而不会破坏它,因此如果需要,您可以稍后重新显示它。 This will save you some time when the button is pressed. 按下按钮可以节省一些时间。 The classes Dialog and DialogBox do this for you automatically when one of their buttons is pressed. DialogDialogBox类会在按下其中一个按钮时自动为您执行此操作。 If you need a window that behaves like a traditional dialog they can a much simpler option that building your own. 如果您需要一个行为类似于传统对话框的窗口,那么构建您自己的窗口就可以更加简单。

Also except in unusual cases you shouldn't need more than one call to MainLoop . 除了在特殊情况下,您不需要多次调用MainLoop When your callback GUI_OPEN_NETLIST returns the MainLoop will resume, explicitly calling MainLoop will likely lead to odd bugs later. 当您的回调GUI_OPEN_NETLIST返回MainLoop将恢复时,显式调用MainLoop可能会导致稍后的奇怪错误。

I think this is close to what your looking for, I haven't tested it though. 我认为这接近你所寻找的,我还没有测试过。

use strict;
use warnings;

my $main = new MainWindow;
my $sidebar = $main->Frame(-relief => "raised", 
                        -borderwidth => 2)
                ->pack (-side=>"left" ,
                        -anchor => "nw", 
                        -fill   => "y");
my $Button1 = $sidebar -> Button (-text=>"Open\nNetlist", 
                               -command=>  \&GUI_OPEN_NETLIST) 
                    ->pack(-fill=>"x");
my $component_dialog = $main->Dialog( -buttons => [ 'Close' ], );

MainLoop;

sub GUI_OPEN_NETLIST
{
    $component_dialog->Show();
}

If you don't want a dialog you should consider if you want to create a second MainWindow or create a Toplevel window dependant on your existing MainWindow . 如果您不想要对话框,则应考虑是否要创建第二个MainWindow或创建依赖于现有MainWindowToplevel窗口。 A Toplevel will close automaticaly when it's MainWindow is closed, a second MainWindow will stay open after the other MainWindow is closed. 一个Toplevel机器会自动关闭时,它的MainWindow被关闭,第二MainWindow后,其他将保持打开MainWindow被关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM