原文: http://zetcode.com/gui/phpgtktutorial/widgets/

在 PHP GTK 编程教程的这一部分中,我们将介绍一些小部件。

小部件是 GUI 应用的基本构建块。 多年来,几个小部件已成为所有 OS 平台上所有工具包中的标准。 例如,按钮,复选框或滚动条。 GTK 工具箱的理念是将小部件的数量保持在最低水平。 将创建更多专门的窗口小部件作为定制 GTK 窗口小部件。

GtkCheckButton

GtkCheckButton是具有两种状态的窗口小部件:开和关。 接通状态通过复选标记显示。 它用来表示一些布尔属性。

  1. <?php
  2. /*
  3. ZetCode PHP GTK tutorial
  4. This program toggles the title of the
  5. window with the GtkCheckButton widget.
  6. author: Jan Bodnar
  7. website: www.zetcode.com
  8. last modified: August 2011
  9. */
  10. class Example extends GtkWindow {
  11. public function __construct() {
  12. parent::__construct();
  13. $this->init_ui();
  14. }
  15. private function init_ui() {
  16. $this->set_title('Check button');
  17. $this->connect_simple('destroy', array('gtk', 'main_quit'));
  18. $fixed = new GtkFixed();
  19. $this->add($fixed);
  20. $cb = new GtkCheckButton("Show title");
  21. $cb->set_active(true);
  22. $cb->connect('clicked', array($this, 'on_clicked'));
  23. $fixed->put($cb, 50, 50);
  24. $this->set_default_size(250, 200);
  25. $this->set_position(GTK::WIN_POS_CENTER);
  26. $this->show_all();
  27. }
  28. public function on_clicked($sender) {
  29. if ($sender->get_active()) {
  30. $this->set_title("Check button");
  31. } else {
  32. $this->set_title("");
  33. }
  34. }
  35. }
  36. new Example();
  37. Gtk::main();
  38. ?>

根据GtkCheckButton的状态,我们将在窗口的标题栏中显示标题。

  1. $cb = new GtkCheckButton("Show title");

GtkCheckButton小部件已创建。 小部件的构造器采用一个参数,即标签。 标签显示在复选框旁边。

  1. $cb->set_active(true);

默认情况下标题是可见的,因此我们默认情况下选中复选按钮。

  1. $cb->connect('clicked', array($this, 'on_clicked'));

如果我们单击复选框小部件,则会发出单击的信号。 我们将on_clicked()方法挂接到信号上。

  1. if ($sender->get_active()) {
  2. $this->set_title("Check button");
  3. } else {
  4. $this->set_title("");
  5. }

如果选中该按钮,我们将显示标题。 get_active()方法用于确定检查按钮的状态。 set_title()方法用于设置窗口的标题。 为了清除窗口的标题,我们使用一个空字符串。

PHP GTK 中的小部件 - 图1

图:GtkCheckButton

GtkLabel

GtkLabel小部件显示文本。 此小部件不支持用户交互。

  1. <?php
  2. /*
  3. ZetCode PHP GTK tutorial
  4. In this example, we show a text on the
  5. window. For this, we use the GtkLabel widget.
  6. author: Jan Bodnar
  7. website: www.zetcode.com
  8. last modified: August 2011
  9. */
  10. class Example extends GtkWindow {
  11. public function __construct() {
  12. parent::__construct();
  13. $this->init_ui();
  14. }
  15. private function init_ui() {
  16. // no trailing white space!
  17. $lyrics = <<<LYRICS
  18. Meet you downstairs in the bar and heard
  19. your rolled up sleeves and your skull t-shirt
  20. You say why did you do it with him today?
  21. and sniff me out like I was Tanqueray
  22. cause you're my fella, my guy
  23. hand me your stella and fly
  24. by the time I'm out the door
  25. you tear men down like Roger Moore
  26. I cheated myself
  27. like I knew I would
  28. I told ya, I was trouble
  29. you know that I'm no good
  30. LYRICS;
  31. $this->set_title('GtkLabel');
  32. $this->connect_simple('destroy', array('gtk', 'main_quit'));
  33. $this->set_border_width(10);
  34. $label = new GtkLabel($lyrics);
  35. $this->add($label);
  36. $this->set_default_size(250, 200);
  37. $this->set_position(GTK::WIN_POS_CENTER);
  38. $this->show_all();
  39. }
  40. }
  41. new Example();
  42. Gtk::main();
  43. ?>

该代码示例在窗口上显示了一些歌词。

  1. // no trailing white space!
  2. $lyrics = <<<LYRICS
  3. Meet you downstairs in the bar and heard
  4. your rolled up sleeves and your skull t-shirt

我们创建多行文本。 在 PHP 中,可以使用 Heredoc 语法创建多行文本。

  1. $this->set_border_width(10);

GtkLabel周围有一些空白。

  1. $label = new GtkLabel($lyrics);
  2. $this->add($label);

GtkLabel小部件已创建并添加到窗口。

PHP GTK 中的小部件 - 图2

图:GtkLabel小部件

GtkEntry

GtkEntry是单行文本输入字段。 该小部件用于输入文本数据。

  1. <?php
  2. /*
  3. ZetCode PHP GTK tutorial
  4. This example demonstrates the GtkEntry widget.
  5. author: Jan Bodnar
  6. website: www.zetcode.com
  7. last modified: August 2011
  8. */
  9. class Example extends GtkWindow {
  10. private $label;
  11. public function __construct() {
  12. parent::__construct();
  13. $this->init_ui();
  14. }
  15. private function init_ui() {
  16. $this->set_title('GtkEntry');
  17. $this->connect_simple('destroy', array('gtk', 'main_quit'));
  18. $fixed = new GtkFixed();
  19. $this->label = new GtkLabel("...");
  20. $fixed->put($this->label, 60, 40);
  21. $entry = new GtkEntry();
  22. $fixed->put($entry, 60, 100);
  23. $entry->connect('key_release_event', array($this, 'on_key_release'));
  24. $this->add($fixed);
  25. $this->set_default_size(250, 200);
  26. $this->set_position(GTK::WIN_POS_CENTER);
  27. $this->show_all();
  28. }
  29. public function on_key_release($sender, $event) {
  30. $this->label->set_text($sender->get_text());
  31. }
  32. }
  33. new Example();
  34. Gtk::main();
  35. ?>

此示例显示了条目小部件和标签。 我们输入的文本将立即显示在标签小部件中。

  1. $entry = new GtkEntry();

GtkEntry小部件已创建。

  1. $entry->connect('key_release_event', array($this, 'on_key_release'));

我们将on_key_release()方法插入GtkEntry小部件的key_release_event

  1. public function on_key_release($sender, $event) {
  2. $this->label->set_text($sender->get_text());
  3. }

在该方法内部,我们通过get_text()方法从GtkEntry小部件中获取文本,并使用标签的set_text()方法将其设置为标签。

PHP GTK 中的小部件 - 图3

图:GtkEntry小部件

GtkImage

GtkImage小部件显示图像。

  1. <?php
  2. /*
  3. ZetCode PHP GTK tutorial
  4. This example demonstrates the GtkImage widget.
  5. author: Jan Bodnar
  6. website: www.zetcode.com
  7. last modified: August 2011
  8. */
  9. class Example extends GtkWindow {
  10. public function __construct() {
  11. parent::__construct();
  12. $this->init_ui();
  13. }
  14. private function init_ui() {
  15. $this->set_title('Red Rock');
  16. $this->connect_simple('destroy', array('gtk', 'main_quit'));
  17. $this->set_border_width(2);
  18. $image = GtkImage::new_from_file("redrock.png");
  19. $this->add($image);
  20. $this->set_default_size(250, 200);
  21. $this->set_position(GTK::WIN_POS_CENTER);
  22. $this->show_all();
  23. }
  24. }
  25. new Example();
  26. Gtk::main();
  27. ?>

在我们的示例中,我们在窗口上显示图像。

  1. $this->set_border_width(2);

我们在图像周围放置了一些空边框。

  1. $image = GtkImage::new_from_file("redrock.png");

GtkImage小部件已创建。 我们使用静态new_from_file()方法从文件加载图像。 如果找不到或无法加载文件,则生成的GtkImage将显示损坏的图像图标。

  1. $this->add($image);

窗口小部件已添加到容器中。

GtkComboBox

ComboBox是一个小部件,允许用户从选项列表中进行选择。

  1. <?php
  2. /*
  3. ZetCode PHP GTK tutorial
  4. This example demonstrates the GtkComboBox widget
  5. author: Jan Bodnar
  6. website: www.zetcode.com
  7. last modified: August 2011
  8. */
  9. class Example extends GtkWindow {
  10. private $label;
  11. public function __construct() {
  12. parent::__construct();
  13. $this->init_ui();
  14. }
  15. private function init_ui() {
  16. $this->set_title('GtkComboBox');
  17. $this->connect_simple('destroy', array('gtk', 'main_quit'));
  18. $fixed = new GtkFixed();
  19. $this->label = new GtkLabel('-');
  20. $fixed->put($this->label, 50, 140);
  21. $cb = GtkComboBox::new_text();
  22. $cb->connect('changed', array($this, 'on_changed'));
  23. $cb->append_text('Ubuntu');
  24. $cb->append_text('Mandriva');
  25. $cb->append_text('Redhat');
  26. $cb->append_text('Gentoo');
  27. $cb->append_text('Mint');
  28. $fixed->put($cb, 50, 30);
  29. $this->add($fixed);
  30. $this->set_default_size(250, 200);
  31. $this->set_position(GTK::WIN_POS_CENTER);
  32. $this->show_all();
  33. }
  34. public function on_changed($sender) {
  35. $this->label->set_label($sender->get_active_text());
  36. }
  37. }
  38. new Example();
  39. Gtk::main();
  40. ?>

该示例显示了一个组合框和一个标签。 组合框具有五个选项的列表。 这些是 Linux 发行版的名称。 标签窗口小部件显示了从组合框中选择的选项。

  1. $cb = GtkComboBox::new_text();

GtkComboBox小部件已创建。 new_text()是创建仅显示字符串的GtkComboBox的方法。

  1. $cb->append_text('Ubuntu');
  2. $cb->append_text('Mandriva');
  3. $cb->append_text('Redhat');
  4. $cb->append_text('Gentoo');
  5. $cb->append_text('Mint');

它充满了数据。

  1. public function on_changed($sender) {
  2. $this->label->set_label($sender->get_active_text());
  3. }

on_changed()方法内部,我们从组合框中获取选定的文本并将其设置为标签。

PHP GTK 中的小部件 - 图4

图:GtkComboBox

在 PHP GTK 教程的这一章中,我们展示了一些基本的小部件。