使用Ruby关闭TK窗口

问题描述 投票:1回答:3

我是Ruby的新手。我能够使用以下代码创建一个带有简单ok / cancel按钮的简单窗口,它可以工作:

require 'tk'

root = TkRoot.new
root.title = "Window"

msgBox = Tk.messageBox(
  'type'    => "okcancel",  
  'icon'    => "info", 
  'title'   => "Framework",
  'message' => "This is message"
)
Tk.mainloop #No operation is performed until closing the TK window
Tk.destroy()
Tk.exit()

虽然我可以手动关闭窗口,但我无法以编程方式关闭窗口。有任何想法吗?

ruby tk
3个回答
1
投票

TK是库,你不应该制作TK.destroy,因为root是TkRoot的对象,它应该如下:

root.destroy()

希望这个答案对其他人有帮助。


0
投票

试试Window.destroyWindow.iconify


0
投票

这对我在Windows 7上使用Ruby 2.2.5(使用Tk 8.5.12)起了作用:

require 'tk'

root = TkRoot.new
root.title = "Window"

msgBox = Tk.messageBox(
  'type'    => "okcancel",  
  'icon'    => "info", 
  'title'   => "Framework",
  'message' => "This is message"
)
if 'ok' == msgBox
  root.destroy
  Kernel.exit
end
Tk.mainloop

TkDocs教程部分Creating and Destroying Windows简单地说“你碰巧破坏了根窗口[,这]将...终结你的应用程序。”但是,为了避免获得Segmentation fault,我需要添加Kernel.exit

© www.soinside.com 2019 - 2024. All rights reserved.