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

发布于 2024-09-02 20:11:33 字数 838 浏览 2 评论 0原文

在我的 Perl/Tk 脚本中,我打开了两个窗口。单击特定按钮后我想关闭其中一个。我怎样才能做到这一点?这是我到目前为止所拥有的:

$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;
}

In my Perl/Tk script I have opened two windows. 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2024-09-09 20:11:33

最简单的方法是在按钮 -command 回调中调用 $component_dialog->destroy 。这样做的缺点是,如果您想稍后重新显示该窗口,则必须重新创建它。
withdraw 方法将隐藏窗口而不破坏它,以便您可以在需要时重新显示它。按下按钮时这将为您节省一些时间。当按下 DialogDialogBox 类中的某个按钮时,它们会自动为您执行此操作。如果您需要一个行为类似于传统对话框的窗口,他们可以提供一个更简单的选项来构建您自己的窗口。

另外,除了特殊情况外,您不需要多次调用 MainLoop。当回调 GUI_OPEN_NETLIST 返回时,MainLoop 将恢复,显式调用 MainLoop 可能会导致稍后出现奇怪的错误。

我认为这很接近你想要的,但我还没有测试过。

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();
}

如果您不需要对话框,则应考虑是否要创建第二个 MainWindow 或创建依赖于现有 MainWindowToplevel 窗口。
MainWindow 关闭时,Toplevel 将自动关闭,第二个 MainWindow 将在另一个 MainWindow 关闭后保持打开状态关闭。

The simplist way is to call $component_dialog->destroy in the buttons -command callback. 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. 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. 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. When your callback GUI_OPEN_NETLIST returns the MainLoop will resume, explicitly calling MainLoop will likely lead to odd bugs later.

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.
A Toplevel will close automaticaly when it's MainWindow is closed, a second MainWindow will stay open after the other MainWindow is closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文