当我在bash终端中运行以下ruby代码时(Ubuntu 14.04,ruby 1.9.3), folder_test工作正常,但my_test显示一个奇怪的行为: 当我从中选择文件夹时,它不会关闭gtk对话框窗口 书签和焦点仍然在对话窗口而不是返回到终端 虽然所选文件夹已正确报告给终端。 出了什么问题,如何强制关闭对话窗口?
# -*- 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
答案 0 :(得分:0)
您可以在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
DIFF:
@@ -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