对话框
在这部分的Ruby GTK教程我们将介绍对话框。
对话框是现代GUI应用程序不可缺的一部分。对话是两个或者更多人之间交谈。在计算机程序中对话是一个用于与应用程序交互的窗口。对话框用于输入数据、修改数据、修改设置等。对话框在用户与计算机程序之间的交流具有重要意义。
消息盒子
消息对话框是应用程序便于给用户提供消息的对话框。消息由文本和图片数据组成。
#!/usr/bin/ruby
# ZetCode Ruby GTK tutorial
#
# This example shows message
# dialogs
#
# author: jan bodnar
# website: www.zetcode.com
# last modified: June 2009
require ‘gtk2’
class RubyApp < Gtk::Window
def initialize
super
set_title “Messages”
signal_connect “destroy” do
Gtk.main_quit
end
init_ui
set_default_size 250, 100
set_window_position Gtk::Window::POS_CENTER
show_all
end
def init_ui
table = Gtk::Table.new 2, 2, true
info = Gtk::Button.new “Information”
warn = Gtk::Button.new “Warning”
ques = Gtk::Button.new “Question”
erro = Gtk::Button.new “Error”
info.signal_connect “clicked” do
on_info
end
warn.signal_connect “clicked” do
on_warn
end
ques.signal_connect “clicked” do
on_ques
end
erro.signal_connect “clicked” do
on_erro
end
table.attach info, 0, 1, 0, 1
table.attach warn, 1, 2, 0, 1
table.attach ques, 0, 1, 1, 2
table.attach erro, 1, 2, 1, 2
add table
end
def on_info
md = Gtk::MessageDialog.new(self,
Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::INFO,
Gtk::MessageDialog::BUTTONS_CLOSE, “Download completed”)
md.run
md.destroy
end
def on_erro
md = Gtk::MessageDialog.new(self, Gtk::Dialog::MODAL |
Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::ERROR,
Gtk::MessageDialog::BUTTONS_CLOSE, “Error loading file”)
md.run
md.destroy
end
def on_ques
md = Gtk::MessageDialog.new(self,
Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::QUESTION,
Gtk::MessageDialog::BUTTONS_CLOSE, “Are you sure to quit?”)
md.run
md.destroy
end
def on_warn
md = Gtk::MessageDialog.new(self,
Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::WARNING,
Gtk::MessageDialog::BUTTONS_CLOSE, “Unallowed operation”)
md.run
md.destroy
end
end
Gtk.init
window = RubyApp.new
Gtk.main
我们的例子中显示了四种消息对话框。信息、警告、询问和错误消息对话框。
info = Gtk::Button.new “Information”
warn = Gtk::Button.new “Warning”
ques = Gtk::Button.new “Question”
erro = Gtk::Button.new “Error”
创建四个按钮。每个按钮将显示不同种类的消息对话框。
def on_info
md = Gtk::MessageDialog.new(self,
Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::INFO,
Gtk::MessageDialog::BUTTONS_CLOSE, “Download completed”)
md.run
md.destroy
end
如果我们点击了info按钮,信息对话框将显示。Gtk::MessageDialog::INFO指定了对话框的类型。Gtk::MessageDialog::BUTTONS_CLOSE指定在对话框中显示的按钮。最后一个参数是要显示的消息。对话框使用run方法显示。程序员必须也要调用destroy或者hide方法。
关于对话框(AboutDialog)
关于对话框显示了应用程序的信息。关于对话框可以显示logo、应用程序名、版本号、版权、网站或者授权信息。它也可能给出作者、文档编写才、翻译者和设计师的信息。
#!/usr/bin/ruby
# ZetCode Ruby GTK tutorial
#
# This example demonstrates the
# AboutDialog dialog
#
# author: jan bodnar
# website: www.zetcode.com
# last modified: June 2009
require ‘gtk2’
class RubyApp < Gtk::Window
def initialize
super
set_title “About dialog”
signal_connect “destroy” do
Gtk.main_quit
end
init_ui
set_default_size 300, 150
set_window_position Gtk::Window::POS_CENTER
show_all
end
def init_ui
button = Gtk::Button.new “About”
button.set_size_request 80, 30
button.signal_connect “clicked” do
on_clicked
end
fix = Gtk::Fixed.new
fix.put button, 20, 20
add fix
end
def on_clicked
about = Gtk::AboutDialog.new
about.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“
about.set_logo Gdk::Pixbuf.new “battery.png”
about.run
about.destroy
end
end
Gtk.init
window = RubyApp.new
Gtk.main
代码伅使用了AboutDialog的一些特性。
about = Gtk::AboutDialog.new
创建一个AboutDialog。
about.set_program_name “Battery”
about.set_version “0.1”
about.set_copyright “(c) Jan Bodnar”
这里我们指定名称、版本号和版权信息。
about.set_logo Gdk::Pixbuf.new “battery.png”
这行创建一个logo。
图片:AboutDialog
字体选择对话框
FontSelectionDialog是一个用于选择字体的对话框。它典型的应用于文本编辑或者格式化的应用程序中。
#!/usr/bin/ruby
# ZetCode Ruby GTK tutorial
#
# This example works with the
# FontSelectionDialog
#
# author: jan bodnar
# website: www.zetcode.com
# last modified: June 2009
require ‘gtk2’
class RubyApp < Gtk::Window
def initialize
super
set_title “FontSelectionDialog”
signal_connect “destroy” do
Gtk.main_quit
end
init_ui
set_default_size 300, 150
set_window_position Gtk::Window::POS_CENTER
show_all
end
def init_ui
set_border_width 10
@label = Gtk::Label.new “The only victory over love is flight.”
button = Gtk::Button.new “Select font”
button.signal_connect “clicked” do
on_clicked
end
fix = Gtk::Fixed.new
fix.put button, 100, 30
fix.put @label, 30, 90
add fix
end
def on_clicked
fdia = Gtk::FontSelectionDialog.new “Select font name”
response = fdia.run
if response == Gtk::Dialog::RESPONSE_OK
font_desc = Pango::FontDescription.new fdia.font_name
if font_desc
@label.modify_font font_desc
end
end
fdia.destroy
end
end
Gtk.init
window = RubyApp.new
Gtk.main
这个代码例子我们创建了一个按钮和一个标签。点击按钮之后显示字体选择对话框。
fdia = Gtk::FontSelectionDialog.new “Select font name”
创建FontSelectionDialog。
if response == Gtk::Dialog::RESPONSE_OK
font_desc = Pango::FontDescription.new fdia.font_name
if font_desc
@label.modify_font font_desc
end
end
如果点击确定按钮,标签的字体会变为我们在对话框中选中的。
图片:FontSelectionDialog
颜色选择对话框
ColorSelectionDialog是用于选择颜色的对话框。
#!/usr/bin/ruby
# ZetCode Ruby GTK tutorial
#
# This example works with the
# ColorSelectionDialog
#
# author: jan bodnar
# website: www.zetcode.com
# last modified: June 2009
require ‘gtk2’
class RubyApp < Gtk::Window
def initialize
super
set_title “ColorSelectionDialog”
signal_connect “destroy” do
Gtk.main_quit
end
init_ui
set_default_size 350, 150
set_window_position Gtk::Window::POS_CENTER
show_all
end
def init_ui
set_border_width 10
@label = Gtk::Label.new “The only victory over love is flight.”
button = Gtk::Button.new “Select color”
button.signal_connect “clicked” do
on_clicked
end
fix = Gtk::Fixed.new
fix.put button, 100, 30
fix.put @label, 30, 90
add fix
end
def on_clicked
cdia = Gtk::ColorSelectionDialog.new “Select color”
response = cdia.run
if response == Gtk::Dialog::RESPONSE_OK
colorsel = cdia.colorsel
color = colorsel.current_color
@label.modify_fg Gtk::STATE_NORMAL, color
end
cdia.destroy
end
end
Gtk.init
window = RubyApp.new
Gtk.main
这个例子与前一个例子很相似。这次我们是改变标签的颜色。
cdia = Gtk::ColorSelectionDialog.new “Select color”
创建ColorSelectionDialog。
if response == Gtk::Dialog::RESPONSE_OK
colorsel = cdia.colorsel
color = colorsel.current_color
@label.modify_fg Gtk::STATE_NORMAL, color
end
如果按下OK按钮,我们得到颜色值并修改标签的颜色。
图片:ColorSelectionDialog
这部分的Ruby GTK教程我们展示了对话框。
原文地址: http://zetcode.com/gui/rubygtk/dialogs/
翻译:龙昌 admin@longchangjin.cn