Ruby 中的 GTK FileChooserDialog 没有关闭 window

GTK FileChooserDialog in Ruby doesn't close window

当我在 bash 终端中 运行 以下 ruby 代码时(Ubuntu 14.04,ruby 1.9.3), folder_test 工作正常,但 my_test 表现出奇怪的行为: 当我从中选择一个文件夹时,它不会关闭 gtk 对话框 window 书签,焦点仍然在对话框 window 而不是返回到终端 尽管已将所选文件夹正确报告给终端。 出了什么问题以及如何强制关闭对话框 window?

# -*- encoding : utf-8 -*- 
#!/usr/bin/ruby

require 'gtk3'

def get_folder(folder)  
  a=''     
  Dir.chdir(File.expand_path(folder)) {
  dialog = Gtk::FileChooserDialog.new(
    :title => "Choose folder", :parent => nil, 
    :action => :select_folder, 
    :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
  if dialog.run == Gtk::ResponseType::ACCEPT
    a=dialog.filename
  end
  dialog.destroy }
  return a 
end

def folder_test
  b=get_folder("/home")
  if b=="/home" 
    puts "No folder chosen"
    exit
  end
  puts "#{b} was choosen."
end

def my_test
  while true do
    folder_test
    puts "Another folder?(y/n)"
    answer=gets.chomp.downcase
    unless answer=='y'
      exit
    end
  end  
end

#folder_test
my_test

可以在dialog.destroy后使用如下代码:

while Gtk.events_pending?
  Gtk.main_iteration
end 

例如:

  if dialog.run == Gtk::ResponseType::ACCEPT
    a=dialog.filename
  end
  dialog.destroy
  while Gtk.events_pending?
    Gtk.main_iteration
  end
  }
  return a
end

差异:

@@ -13,7 +13,11 @@ def get_folder(folder)
   if dialog.run == Gtk::ResponseType::ACCEPT
     a=dialog.filename
   end 
-  dialog.destroy }
+  dialog.destroy
+  while Gtk.events_pending?
+    Gtk.main_iteration
+  end 
+  }
   return a
 end