如何关闭 Perl/Tk 中的窗口?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是在按钮
-command
回调中调用$component_dialog->destroy
。这样做的缺点是,如果您想稍后重新显示该窗口,则必须重新创建它。withdraw
方法将隐藏窗口而不破坏它,以便您可以在需要时重新显示它。按下按钮时这将为您节省一些时间。当按下Dialog
和DialogBox
类中的某个按钮时,它们会自动为您执行此操作。如果您需要一个行为类似于传统对话框的窗口,他们可以提供一个更简单的选项来构建您自己的窗口。另外,除了特殊情况外,您不需要多次调用
MainLoop
。当回调 GUI_OPEN_NETLIST 返回时,MainLoop 将恢复,显式调用 MainLoop 可能会导致稍后出现奇怪的错误。我认为这很接近你想要的,但我还没有测试过。
如果您不需要对话框,则应考虑是否要创建第二个
MainWindow
或创建依赖于现有MainWindow
的Toplevel
窗口。当
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 classesDialog
andDialogBox
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 callingMainLoop
will likely lead to odd bugs later.I think this is close to what your looking for, I haven't tested it though.
If you don't want a dialog you should consider if you want to create a second
MainWindow
or create aToplevel
window dependant on your existingMainWindow
.A
Toplevel
will close automaticaly when it'sMainWindow
is closed, a secondMainWindow
will stay open after the otherMainWindow
is closed.