在 Ruby GTK 编程教程的这一部分中,我们将介绍对话框。
对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。
MessageDialog
消息对话框是方便的对话框,可向应用的用户提供消息。 该消息包含文本和图像数据。
#!/usr/bin/ruby'''ZetCode Ruby GTK tutorialThis example shows message dialogs.Author: Jan BodnarWebsite: www.zetcode.comLast modified: May 2014'''require 'gtk3'class RubyApp < Gtk::Windowdef initializesuperinit_uienddef init_uitable = Gtk::Table.new 2, 2, trueinfo = Gtk::Button.new :label => "Information"warn = Gtk::Button.new :label => "Warning"ques = Gtk::Button.new :label => "Question"erro = Gtk::Button.new :label => "Error"info.signal_connect "clicked" doon_infoendwarn.signal_connect "clicked" doon_warnendques.signal_connect "clicked" doon_quesenderro.signal_connect "clicked" doon_erroendtable.attach info, 0, 1, 0, 1table.attach warn, 1, 2, 0, 1table.attach ques, 0, 1, 1, 2table.attach erro, 1, 2, 1, 2add tableset_title "Messages"signal_connect "destroy" doGtk.main_quitendset_default_size 300, 100set_window_position :centershow_allenddef on_infomd = Gtk::MessageDialog.new :parent => self,:flags => :destroy_with_parent, :type => :info,:buttons_type => :close, :message => "Download completed"md.runmd.destroyenddef on_erromd = Gtk::MessageDialog.new :parent => self,:flags => :modal, :type => :error,:buttons_type => :close, :message => "Error loading file"md.runmd.destroyenddef on_quesmd = Gtk::MessageDialog.new :parent => self,:flags => :destroy_with_parent, :type => :question,:buttons_type => :close, :message => "Are you sure to quit?"md.runmd.destroyenddef on_warnmd = Gtk::MessageDialog.new :parent => self,:flags => :destroy_with_parent, :type => :warning,:buttons_type => :close, :message => "Unallowed operation"md.runmd.destroyendendGtk.initwindow = RubyApp.newGtk.main
在我们的示例中,我们将显示四种消息对话框:信息,警告,问题和错误消息对话框。
info = Gtk::Button.new :label => "Information"warn = Gtk::Button.new :label => "Warning"ques = Gtk::Button.new :label => "Question"erro = Gtk::Button.new :label => "Error"
我们有四个按钮。 这些按钮中的每个按钮都会显示不同类型的消息对话框。
def on_infomd = Gtk::MessageDialog.new :parent => self,:flags => :destroy_with_parent, :type => :info,:buttons_type => :close, :message => "Download completed"md.runmd.destroyend
如果单击信息按钮,则会显示信息对话框。 Gtk::MessageDialog::INFO指定对话框的类型。 Gtk::MessageDialog::BUTTONS_CLOSE指定要在对话框中显示的按钮的类型。 最后一个参数是显示的消息。 该对话框使用run方法显示。 程序员还必须调用destroy或hide方法。




AboutDialog
AboutDialog显示有关应用的信息。 它可以显示徽标,应用名称,版本,版权,网站或许可证信息。 也有可能对作者,文档撰写者,翻译者和艺术家予以赞扬。
#!/usr/bin/ruby'''ZetCode Ruby GTK tutorialThis example demonstrates theGtk::AboutDialog dialog.Author: Jan BodnarWebsite: www.zetcode.comLast modified: May 2014'''require 'gtk3'class RubyApp < Gtk::Windowdef initializesuperset_title "About dialog"signal_connect "destroy" doGtk.main_quitendinit_uiset_default_size 300, 150set_window_position :centershow_allenddef init_uibutton = Gtk::Button.new :label => "About"button.set_size_request 80, 30button.signal_connect "clicked" doon_clickedendfix = Gtk::Fixed.newfix.put button, 20, 20add fixenddef on_clickedabout = Gtk::AboutDialog.newabout.set_program_name "Battery"about.set_version "0.1"about.set_copyright "(c) Jan Bodnar"about.set_comments "Battery is a simple tool for battery checking"about.set_website "http://www.zetcode.com"beginlogo = Gdk::Pixbuf.new :file => "batter.png"about.set_logo logorescue IOError => eputs eputs "cannot load image"exitendabout.runabout.destroyendendGtk.initwindow = RubyApp.newGtk.main
该代码示例使用具有某些功能的Gtk::AboutDialog。
about = Gtk::AboutDialog.new
我们创建Gkt::AboutDialog小部件。
about.set_program_name "Battery"about.set_version "0.1"about.set_copyright "(c) Jan Bodnar"
在这里,我们指定名称,版本和版权。
beginlogo = Gdk::Pixbuf.new :file => "batter.png"about.set_logo logorescue IOError => eputs eputs "cannot load image"exitend
此行为对话框创建徽标。 执行一些错误检查。

图:Gtk::AboutDialog
Gtk::FontSelectionDialog
Gtk::FontSelectionDialog是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。
#!/usr/bin/ruby'''ZetCode Ruby GTK tutorialThis example presents the Gtk::FontSelectionDialog.Author: Jan BodnarWebsite: www.zetcode.comLast modified: May 2014'''require 'gtk3'class RubyApp < Gtk::Windowdef initializesuperinit_uienddef init_uiset_border_width 10@label = Gtk::Label.new "The only victory over love is flight."button = Gtk::Button.new :label => "Select font"button.signal_connect "clicked" doon_clickedendfix = Gtk::Fixed.newfix.put button, 100, 30fix.put @label, 30, 90add fixset_title "Gtk::FontSelectionDialog"signal_connect "destroy" doGtk.main_quitendset_default_size 300, 150set_window_position :centershow_allenddef on_clickedfdia = Gtk::FontChooserDialog.new :title => "Select font name",:parent => nilresponse = fdia.runif response == Gtk::ResponseType::OKfont_desc = Pango::FontDescription.new fdia.font_descif font_desc@label.override_font font_descendendfdia.destroyendendGtk.initwindow = RubyApp.newGtk.main
在代码示例中,我们有一个按钮和一个标签。 单击按钮显示Gtk::FontSelectionDialog。
fdia = Gtk::FontSelectionDialog.new "Select font name"
我们创建GtkFontSelectionDialog。
if response == Gtk::ResponseType::OKfont_desc = Pango::FontDescription.new fdia.font_descif font_desc@label.override_font font_descendend
如果单击“确定”按钮,则标签小部件的字体将更改为我们在对话框中选择的字体。
Gtk::ColorSelectionDialog
Gtk::ColorSelectionDialog是用于选择颜色的对话框。
#!/usr/bin/ruby'''ZetCode Ruby GTK tutorialThis example presents the Gtk::ColorSelectionDialog.Author: Jan BodnarWebsite: www.zetcode.comLast modified: May 2014'''require 'gtk3'class RubyApp < Gtk::Windowdef initializesuperinit_uienddef init_uiset_border_width 10@label = Gtk::Label.new "The only victory over love is flight."button = Gtk::Button.new :label => "Select colour"button.signal_connect "clicked" doon_clickedendfix = Gtk::Fixed.newfix.put button, 100, 30fix.put @label, 30, 90add fixset_title "Gtk::ColorSelectionDialog"signal_connect "destroy" doGtk.main_quitendset_default_size 350, 150set_window_position :centershow_allenddef on_clickedcdia = Gtk::ColorSelectionDialog.new :title => "Select colour"response = cdia.runif response == Gtk::ResponseType::OKcolorsel = cdia.color_selectioncol = colorsel.current_rgba@label.override_color :normal, colendcdia.destroyendendGtk.initwindow = RubyApp.newGtk.main
该示例与上一个示例非常相似。 这次我们更改标签的颜色。
cdia = Gtk::ColorSelectionDialog.new :title => "Select colour"
我们创建Gtk::ColorSelectionDialog。
if response == Gtk::ResponseType::OKcolorsel = cdia.color_selectioncol = colorsel.current_rgba@label.override_color :normal, colend
如果按“确定”按钮,我们将获得颜色值并修改标签的颜色。

图:Gtk::ColorSelectionDialog
在 Ruby GTK 教程的这一部分中,我们介绍了对话框。
