Ruby/TK - Standard Dailog Box



Description

Dialog boxes are a type of window used in applications to get some information from the user, inform them that some event has occurred, confirm an action and more.

The appearance and usage of dialog boxes is usually quite specifically detailed in a platform's style guide. Tk comes with a number of dialog boxes built-in for common tasks, and which help you conform to platform specific style guidelines.

File, Directory and Color Dialog Box

Ruby/Tk provides several dialogs to let the user select files or directories. The open variant on the dialog is used when you want the user to select an existing file, while the save variant is used to choose a file to save. There are four variants, which can be used −

  • Tk.getOpenFile − To have one open file dialog box.

  • Tk.getSaveFile − To have one save file dialog box.

  • Tk.chooseDirectory − To have one choose directory dialog box.

  • Tk.chooseColor − To have one choose color dialog box.

Examples

Following example will explain how to create Open file dialog box.

require 'tk'

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

button_click = Proc.new {
   Tk.getOpenFile
}

button = TkButton.new(root) do
   text "button"
   pack("side" => "left",  "padx"=> "50", "pady"=> "50")
end

button.comman = button_click

Tk.mainloop

This will produce the following result −

Ruby/Tk Open Box

Following example will explain how to create Choose Color dialog box.

require 'tk'

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

button_click = Proc.new {
   Tk.chooseColor
}

button = TkButton.new(root) do
   text "button"
   pack("side" => "left",  "padx"=> "50", "pady"=> "50")
end

button.comman = button_click

Tk.mainloop

This will produce the following result −

Ruby/Tk Clor Box
ruby_tk_guide.htm
Advertisements