原文: http://zetcode.com/gui/javascriptgtktutorial/dialogs/

在 JavaScript GTK 编程教程的这一部分中,我们将介绍对话框。

对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。

MessageDialog

消息对话框是方便的对话框,可向应用的用户提供消息。 该消息包含文本和图像数据。

  1. #!/usr/bin/seed
  2. /*
  3. ZetCode JavaScript GTK tutorial
  4. This example demonstrates a
  5. Message dialog
  6. author: Jan Bodnar
  7. website: www.zetcode.com
  8. last modified: July 2011
  9. */
  10. Gtk = imports.gi.Gtk;
  11. Gtk.init(null, null);
  12. Example = new GType({
  13. parent: Gtk.Window.type,
  14. name: "Example",
  15. init: function ()
  16. {
  17. init_ui(this);
  18. function init_ui(w) {
  19. w.signal.hide.connect(Gtk.main_quit);
  20. w.set_default_size(250, 200);
  21. w.set_title("Message dialog");
  22. w.set_position(Gtk.WindowPosition.CENTER);
  23. var fixed = new Gtk.Fixed();
  24. var infoButton = new Gtk.Button.with_label("Information");
  25. fixed.put(infoButton, 30, 20);
  26. infoButton.signal.clicked.connect(on_info);
  27. w.add(fixed);
  28. w.show_all();
  29. }
  30. function on_info() {
  31. var md = new Gtk.MessageDialog({modal:true, title:"Information",
  32. message_type:Gtk.MessageType.INFO,
  33. buttons:Gtk.ButtonsType.OK, text:"Download completed."});
  34. md.run();
  35. md.destroy();
  36. }
  37. }
  38. });
  39. var window = new Example();
  40. Gtk.main();

我们在窗口上显示一个按钮。 当我们单击按钮时,会显示一条信息消息。

  1. var infoButton = new Gtk.Button.with_label("Information");

这是一个按钮,当我们单击它时将显示一个对话框。

  1. function on_info() {
  2. var md = new Gtk.MessageDialog({modal:true, title:"Information",
  3. message_type:Gtk.MessageType.INFO,
  4. buttons:Gtk.ButtonsType.OK, text:"Download completed."});
  5. md.run();
  6. md.destroy();
  7. }

如果单击信息按钮,将显示“信息”对话框。 Gtk.MessageType.INFO指定对话框的类型。 Gtk.ButtonsType.OK指定对话框中将显示哪些按钮。 最后一个参数是显示的消息。 该对话框使用run()方法显示。 程序员还必须调用destroy()hide()方法。

JavaScript GTK 中的对话框 - 图1

图:MessageDialog

AboutDialog

AboutDialog显示有关应用的信息。 它可以显示徽标,应用名称,版本,版权,网站或许可证信息。 也有可能对作者,文档撰写者,翻译者和艺术家予以赞扬。

  1. #!/usr/bin/seed
  2. /*
  3. ZetCode JavaScript GTK tutorial
  4. This example demonstrates the
  5. AboutDialog dialog.
  6. author: Jan Bodnar
  7. website: www.zetcode.com
  8. last modified: July 2011
  9. */
  10. Gtk = imports.gi.Gtk;
  11. GdkPixbuf = imports.gi.GdkPixbuf;
  12. Gtk.init(null, null);
  13. Example = new GType({
  14. parent: Gtk.Window.type,
  15. name: "Example",
  16. init: function ()
  17. {
  18. init_ui(this);
  19. function init_ui(w) {
  20. w.signal.hide.connect(Gtk.main_quit);
  21. w.set_default_size(250, 200);
  22. w.set_title("About dialog");
  23. w.set_position(Gtk.WindowPosition.CENTER);
  24. var button = new Gtk.Button.with_label("About");
  25. button.set_size_request(80, 30);
  26. button.signal.clicked.connect(on_clicked);
  27. var fix = new Gtk.Fixed();
  28. fix.put(button, 20, 20);
  29. w.add(fix);
  30. w.show_all();
  31. }
  32. function on_clicked() {
  33. var about = new Gtk.AboutDialog();
  34. about.set_program_name("Battery");
  35. about.set_version("0.1");
  36. about.set_copyright("(c) Jan Bodnar");
  37. about.set_comments("Battery is a simple tool for battery checking");
  38. about.set_website("http://www.zetcode.com");
  39. about.set_logo(new GdkPixbuf.Pixbuf.from_file("battery.png"));
  40. about.run();
  41. about.destroy();
  42. }
  43. }
  44. });
  45. var window = new Example();
  46. Gtk.main();

该代码示例使用具有某些功能的AboutDialog

  1. var about = new Gtk.AboutDialog();

我们创建AboutDialog的实例。

  1. about.set_program_name("Battery");
  2. about.set_version("0.1");
  3. about.set_copyright("(c) Jan Bodnar");

在这里,我们指定程序的名称,版本和版权。

  1. about.set_logo(new GdkPixbuf.Pixbuf.from_file("battery.png"));

此行创建徽标。

JavaScript GTK 中的对话框 - 图2

图:AboutDialog

FontSelectionDialog

FontSelectionDialog是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。

  1. #!/usr/bin/seed
  2. /*
  3. ZetCode JavaScript GTK tutorial
  4. This example works with the
  5. FontSelectionDialog.
  6. author: Jan Bodnar
  7. website: www.zetcode.com
  8. last modified: July 2011
  9. */
  10. Gtk = imports.gi.Gtk;
  11. Pango = imports.gi.Pango;
  12. Gtk.init(null, null);
  13. Example = new GType({
  14. parent: Gtk.Window.type,
  15. name: "Example",
  16. init: function ()
  17. {
  18. init_ui(this);
  19. var label;
  20. function init_ui(w) {
  21. w.signal.hide.connect(Gtk.main_quit);
  22. w.set_default_size(350, 150);
  23. w.set_title("Font selection");
  24. w.set_position(Gtk.WindowPosition.CENTER);
  25. w.set_border_width(10);
  26. var text = "The only victory over love is flight."
  27. label = new Gtk.Label.c_new(text);
  28. var button = new Gtk.Button.with_label("Select font");
  29. button.signal.clicked.connect(on_clicked);
  30. var fix = new Gtk.Fixed();
  31. fix.put(button, 100, 30);
  32. fix.put(label, 30, 90);
  33. w.add(fix);
  34. w.show_all();
  35. }
  36. function on_clicked() {
  37. var fdia = new Gtk.FontSelectionDialog.c_new("Select font");
  38. var response = fdia.run();
  39. if (response == Gtk.ResponseType.OK) {
  40. var fname = fdia.get_font_name();
  41. var font_desc = Pango.Font.description_from_string(fname);
  42. if (font_desc)
  43. label.modify_font(font_desc);
  44. }
  45. fdia.destroy();
  46. }
  47. }
  48. });
  49. var window = new Example();
  50. Gtk.main();

在代码示例中,我们有一个按钮和一个标签。 单击按钮显示FontSelectionDialog

  1. var fdia = new Gtk.FontSelectionDialog.c_new("Select font");

我们创建FontSelectionDialog

  1. if (response == Gtk.ResponseType.OK) {
  2. var fname = fdia.get_font_name();
  3. var font_desc = Pango.Font.description_from_string(fname);
  4. if (font_desc)
  5. label.modify_font(font_desc);
  6. }

如果单击“确定”按钮,则标签小部件的字体将更改为我们在对话框中选择的字体。

JavaScript GTK 中的对话框 - 图3

图:FontSelectionDialog

在 JavaScript GTK 教程的这一部分中,我们介绍了对话框。