I am using the ruby gem gtk2
to display a simple file dialog, where the user can select a file from his computer. It works just fine, however there is one little problem.
The file dialog doesnt close after selecting the file, but it stays open until the whole script finished.
My file dialog looks like this:
def ask_for_file(question)
dialog = Gtk::FileChooserDialog.new(question, nil,
Gtk::FileChooser::ACTION_OPEN,
"gnome-vfs",
[Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT],
[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]
)
if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
file_name = dialog.filename.to_s
return file_name
else
return nil
end
end
When I call it in my script like this:
path = ask_for_file("Test?")
sleep(5)
puts "continue with #{path}"
The file dialog stays open for 5 seconds and is unresponsive in that time. How could I get it to close after a file has been selected, but before the 5 second sleep?
I tried to use dialog.destroy
in ask_for_file
just before returning the file_name
but that didnt seem to help.
I solved it by creating a new window with a button to open the file dialog. Upon clicking the open button, I also send a signal to destroy the main window. This way everything closes immediatly after I selected a file.
For reference see this example