php+GTK2 学习第二篇
继续学习PHP+GTK,这次实际搞东西了。又学习了两天,博主在这做一下记录。
推荐一篇国外的文章:http://zetcode.com/gui/phpgtktutorial/introduction/
一定要看这篇文章!
一定要看这篇文章!!
一定要看这篇文章!!!
博主英文不好,高中开始对英语就发虚,还好浏览器自带翻译,开双页面,对照着看 :),这篇文章包含了大量的例子,绝对是入门的不二选择。最快的学习方法,把里面的代码copy下来,调用,一边看效果一边看代码。
算了,我给你们抄过来吧! 做代码的搬运工
1、小例子
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This program centers a window on 7 the screen. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: September 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 22 $this->set_title('Simple'); //标题 23 $this->set_default_size(250, 150); //大小控制 24 25 $this->connect_simple('destroy', array('gtk', 'main_quit')); 26 27 $this->set_position(GTK::WIN_POS_CENTER); //居中显示 28 $this->show(); 29 } 30 } 31 32 new Example(); 33 Gtk::main();
效果如下:
2、简单按钮
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This code shows a tooltip on 7 a window and a button. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: September 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 23 } 24 25 public function init_ui() { 26 27 $this->set_title('Tooltips'); 28 $this->connect_simple('destroy', array('gtk', 'main_quit')); 29 30 $fixed = new GtkFixed(); //灵活定位 31 $this->add($fixed); 32 33 $button = new GtkButton("Button"); //实例化按钮 34 $button->set_size_request(80, 35); //大小控制 35 $button->set_tooltip_text("Button widget"); //鼠标放上显示 类似hover 36 37 $fixed->put($button, 50, 50); //定位 38 39 $this->set_tooltip_text("Window widget"); 40 41 $this->set_default_size(250, 150); 42 $this->set_position(GTK::WIN_POS_CENTER); 43 $this->show_all(); 44 } 45 } 46 47 new Example(); 48 Gtk::main();
效果:
3、引入图片
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 In this program, we lay out widgets 7 using absolute positioning. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: September 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 23 } 24 25 public function init_ui() { 26 27 $this->set_title('Fixed'); 28 $this->connect_simple('destroy', array('gtk', 'main_quit')); 29 30 $this->modify_bg(Gtk::STATE_NORMAL, new GdkColor(6400, 6400, 6440)); //设置大背景色 31 32 $bardejov = GtkImage::new_from_file("./demos/gnu-keys.png"); 33 $rotunda = GtkImage::new_from_file("./demos/gnome-gimp.png");//引入图片 注意路径 不是php文件位置 而是从php.exe这开始的 34 35 $fixed = new GtkFixed(); 36 $fixed->put($bardejov, 20, 20); 37 $fixed->put($rotunda, 40, 160); 38 39 $this->add($fixed); 40 41 42 $this->set_default_size(300, 280); 43 $this->set_position(GTK::WIN_POS_CENTER); 44 $this->show_all(); 45 46 } 47 } 48 49 new Example(); 50 Gtk::main(); 51
效果:
4、UI盒子容器布局
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 In this program, we position two buttons 7 in the bottom right corner of the window. 8 We use horizontal and vertical boxes. 9 10 author: Jan Bodnar 11 website: www.zetcode.com 12 last modified: August 2011 13 */ 14 15 class Example extends GtkWindow { 16 17 18 public function __construct() { 19 20 parent::__construct(); 21 22 $this->init_ui(); 23 24 } 25 26 public function init_ui() { 27 28 $this->set_title('Buttons'); 29 $this->connect_simple('destroy', array('gtk', 'main_quit')); 30 $this->set_border_width(3); 31 32 $vbox = new GtkVBox(false, 5); //竖向盒子容器 33 $hbox = new GtkHBox(true, 3); //横向盒子容器 34 35 $frame = new GtkFrame(); //空边框 36 $vbox->pack_start($frame, true, true, 0); 37 38 $okButton = new GtkButton("OK"); 39 $okButton->set_size_request(70, 30); 40 $closeButton = new GtkButton("Close"); 41 42 $hbox->add($okButton); 43 $hbox->add($closeButton); 44 45 $halign = new GtkAlignment(1, 0, 0, 0); 46 $halign->add($hbox); 47 $vbox->pack_start($halign, false, false, 3); 48 49 $this->add($vbox); 50 51 $this->set_default_size(260, 150); 52 $this->set_position(GTK::WIN_POS_CENTER); 53 $this->show_all(); 54 } 55 } 56 57 new Example(); 58 Gtk::main();
效果:
5、计算器布局
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 In this program we create a skeleton of 7 a calculator. We use the GtkTable widget. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: August 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 23 } 24 25 public function init_ui() { 26 27 $this->set_title('Calculator'); 28 $this->connect_simple('destroy', array('gtk', 'main_quit')); 29 30 $vbox = new GtkVBox(false, 2); 31 32 $mb = new GtkMenubar(); 33 $filemenu = new GtkMenu(); //菜单 34 $filemi = new GtkMenuItem("File"); //项目 35 $filemi->set_submenu($filemenu); 36 $mb->append($filemi); 37 38 $vbox->pack_start($mb, false, false, 0); 39 40 $table = new GtkTable(5, 4, true); 41 42 $table->attach_defaults(new GtkButton("Cls"), 0, 1, 0, 1); 43 $table->attach_defaults(new GtkButton("Bck"), 1, 2, 0, 1); 44 $table->attach_defaults(new GtkLabel(), 2, 3, 0, 1); 45 $table->attach_defaults(new GtkButton("Close"), 3, 4, 0, 1); 46 47 $table->attach_defaults(new GtkButton("7"), 0, 1, 1, 2); 48 $table->attach_defaults(new GtkButton("8"), 1, 2, 1, 2); 49 $table->attach_defaults(new GtkButton("9"), 2, 3, 1, 2); 50 $table->attach_defaults(new GtkButton("/"), 3, 4, 1, 2); 51 52 $table->attach_defaults(new GtkButton("4"), 0, 1, 2, 3); 53 $table->attach_defaults(new GtkButton("5"), 1, 2, 2, 3); 54 $table->attach_defaults(new GtkButton("6"), 2, 3, 2, 3); 55 $table->attach_defaults(new GtkButton("*"), 3, 4, 2, 3); 56 57 $table->attach_defaults(new GtkButton("1"), 0, 1, 3, 4); 58 $table->attach_defaults(new GtkButton("2"), 1, 2, 3, 4); 59 $table->attach_defaults(new GtkButton("3"), 2, 3, 3, 4); 60 $table->attach_defaults(new GtkButton("-"), 3, 4, 3, 4); 61 62 $table->attach_defaults(new GtkButton("0"), 0, 1, 4, 5); 63 $table->attach_defaults(new GtkButton("."), 1, 2, 4, 5); 64 $table->attach_defaults(new GtkButton("="), 2, 3, 4, 5); 65 $table->attach_defaults(new GtkButton("+"), 3, 4, 4, 5); 66 67 $vbox->pack_start(new GtkEntry(), false, false, 0); 68 $vbox->pack_end($table, true, true, 0); 69 70 $this->add($vbox); 71 72 $this->set_default_size(300, 250); 73 $this->set_position(GTK::WIN_POS_CENTER); 74 $this->show_all(); 75 76 } 77 } 78 79 new Example(); 80 Gtk::main();
效果:
6、table布局
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This is a more complicated layout example. 7 We use GtkAlignment and GtkTable widgets. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: August 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 } 23 24 public function init_ui() { 25 26 $this->set_title('Windows'); 27 $this->connect_simple('destroy', array('gtk', 'main_quit')); 28 29 $this->set_border_width(15); 30 31 $table = new GtkTable(8, 4, false); 32 $table->set_col_spacings(3); 33 34 $title = new GtkLabel("Windows"); 35 36 $halign = new GtkAlignment(0, 0, 0, 0); 37 $halign->add($title); 38 $table->attach($halign, 0, 1, 0, 1, GTK::FILL, 39 GTK::FILL, 0, 0); 40 41 $frame = new GtkFrame(); 42 $table->attach($frame, 0, 2, 1, 3, GTK::FILL | Gtk::EXPAND, 43 GTK::FILL | GTK::EXPAND, 1, 1); 44 45 $activate = new GtkButton("Activate"); 46 $activate->set_size_request(50, 30); 47 $table->attach($activate, 3, 4, 1, 2, GTK::FILL, 48 GTK::SHRINK, 1, 1); 49 50 $valign = new GtkAlignment(0, 0, 0, 0); 51 $close = new GtkButton("Close"); 52 $close->set_size_request(70, 30); 53 $valign->add($close); 54 $table->set_row_spacing(1, 3); 55 $table->attach($valign, 3, 4, 2, 3, Gtk::FILL, 56 Gtk::FILL | Gtk::EXPAND, 1, 1); 57 58 $halign2 = new GtkAlignment(0, 1, 0, 0); 59 $help = new GtkButton("Help"); 60 $help->set_size_request(70, 30); 61 $halign2->add($help); 62 $table->set_row_spacing(3, 6); 63 $table->attach($halign2, 0, 1, 4, 5, Gtk::FILL, 64 Gtk::FILL, 0, 0); 65 66 $ok = new GtkButton("OK"); 67 $ok->set_size_request(70, 30); 68 $table->attach($ok, 3, 4, 4, 5, Gtk::FILL, 69 Gtk::FILL, 0, 0); 70 71 $this->add($table); 72 73 $this->set_default_size(300, 250); 74 $this->set_position(GTK::WIN_POS_CENTER); 75 $this->show_all(); 76 } 77 } 78 79 new Example(); 80 Gtk::main();
效果:
7、单选框
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This program toggles the title of the 7 window with the GtkCheckButton widget. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: August 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 23 } 24 25 private function init_ui() { 26 27 $this->set_title('Check button'); 28 $this->connect_simple('destroy', array('gtk', 'main_quit')); 29 30 $fixed = new GtkFixed(); 31 $this->add($fixed); 32 33 $cb = new GtkCheckButton("Show title"); 34 $cb->set_active(true); 35 $cb->connect('clicked', array($this, 'on_clicked')); 36 $fixed->put($cb, 50, 50); 37 38 $this->set_default_size(250, 200); 39 $this->set_position(GTK::WIN_POS_CENTER); 40 $this->show_all(); 41 } 42 43 public function on_clicked($sender) { 44 45 if ($sender->get_active()) { 46 $this->set_title("Check button"); 47 } else { 48 $this->set_title(""); 49 } 50 } 51 } 52 53 new Example(); 54 Gtk::main();
效果:
8、输入框
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This example demonstrates the GtkEntry widget. 7 8 author: Jan Bodnar 9 website: www.zetcode.com 10 last modified: August 2011 11 */ 12 13 class Example extends GtkWindow { 14 15 private $label; 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 23 } 24 25 private function init_ui() { 26 27 $this->set_title('GtkEntry'); 28 $this->connect_simple('destroy', array('gtk', 'main_quit')); 29 30 $fixed = new GtkFixed(); 31 32 $this->label = new GtkLabel("..."); 33 $fixed->put($this->label, 60, 40); 34 35 $entry = new GtkEntry(); 36 $fixed->put($entry, 60, 100); 37 $entry->connect('key_release_event', array($this, 'on_key_release')); 38 39 $this->add($fixed); 40 41 $this->set_default_size(250, 200); 42 $this->set_position(GTK::WIN_POS_CENTER); 43 $this->show_all(); 44 } 45 46 public function on_key_release($sender, $event) { 47 48 $this->label->set_text($sender->get_text()); 49 } 50 } 51 52 new Example(); 53 Gtk::main();
效果:
这里注意下,我感觉输入框不好使,一开始点不进去,要缩小再放大下才能光标选中开始输入,应该还有点问题
9、复选框
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This example demonstrates the GtkComboBox widget 7 8 author: Jan Bodnar 9 website: www.zetcode.com 10 last modified: August 2011 11 */ 12 13 class Example extends GtkWindow { 14 15 private $label; 16 17 public function __construct() { 18 19 parent::__construct(); 20 21 $this->init_ui(); 22 23 } 24 25 private function init_ui() { 26 27 $this->set_title('GtkComboBox'); 28 $this->connect_simple('destroy', array('gtk', 'main_quit')); 29 30 $fixed = new GtkFixed(); 31 $this->label = new GtkLabel('-'); 32 $fixed->put($this->label, 50, 140); 33 34 $cb = GtkComboBox::new_text(); 35 $cb->connect('changed', array($this, 'on_changed')); 36 37 $cb->append_text('Ubuntu'); 38 $cb->append_text('Mandriva'); 39 $cb->append_text('Redhat'); 40 $cb->append_text('Gentoo'); 41 $cb->append_text('Mint'); 42 43 $fixed->put($cb, 50, 30); 44 45 $this->add($fixed); 46 47 $this->set_default_size(250, 200); 48 $this->set_position(GTK::WIN_POS_CENTER); 49 $this->show_all(); 50 } 51 52 public function on_changed($sender) { 53 $this->label->set_label($sender->get_active_text()); 54 } 55 } 56 57 new Example(); 58 Gtk::main();
效果:
10.菜单定制
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This example shows a submenu. 7 8 author: Jan Bodnar 9 website: www.zetcode.com 10 last modified: August 2011 11 */ 12 13 class Example extends GtkWindow { 14 15 16 public function __construct() { 17 18 parent::__construct(); 19 20 $this->init_ui(); 21 22 } 23 24 public function init_ui() { 25 26 $this->set_title('Submenu'); 27 $this->connect_simple('destroy', array('gtk', 'main_quit')); 28 29 $this->modify_bg(Gtk::STATE_NORMAL, new GdkColor(6400, 6400, 6440)); 30 31 $mb = new GtkMenuBar(); 32 33 $filemenu = new GtkMenu(); 34 $filemi = new GtkMenuItem("File"); 35 $filemi->set_submenu($filemenu); 36 37 $mb->append($filemi); 38 39 $imenu = new GtkMenu(); 40 41 $importm = new GtkMenuItem("Import"); 42 $importm->set_submenu($imenu); 43 44 $inews = new GtkMenuItem("Import news feed..."); 45 $ibookmarks = new GtkMenuItem("Import bookmarks..."); 46 $imail = new GtkMenuItem("Import mail..."); 47 48 $imenu->append($inews); 49 $imenu->append($ibookmarks); 50 $imenu->append($imail); 51 52 $filemenu->append($importm); 53 54 $exitmi = new GtkMenuItem("Exit"); 55 $exitmi->connect_simple('activate', array('gtk', 'main_quit')); 56 57 $filemenu->append($exitmi); 58 59 $vbox = new GtkVBox(false, 2); 60 $vbox->pack_start($mb, false, false, 0); 61 62 $this->add($vbox); 63 64 $this->set_default_size(320, 250); 65 $this->set_position(GTK::WIN_POS_CENTER); 66 $this->show_all(); 67 } 68 } 69 70 new Example(); 71 Gtk::main();
效果:
11.提示框
1 <?php 2 3 /* 4 ZetCode PHP GTK tutorial 5 6 This example demonstrates a 7 GtkMessageDialog. 8 9 author: Jan Bodnar 10 website: www.zetcode.com 11 last modified: September 2011 12 */ 13 14 class Example extends GtkWindow { 15 16 public function __construct() { 17 18 parent::__construct(); 19 20 $this->init_ui(); 21 22 } 23 24 public function init_ui() { 25 26 $this->set_title('GtkMessageDialog'); 27 $this->connect_simple('destroy', array('gtk', 'main_quit')); 28 29 $fixed = new GtkFixed(); 30 31 $button = new GtkButton("Information"); 32 $button->set_size_request($button->size_request()); 33 $button->connect('clicked', array($this, 'on_clicked')); 34 35 $fixed->put($button, 50, 50); 36 $this->add($fixed); 37 38 $this->set_default_size(250, 200); 39 $this->set_position(GTK::WIN_POS_CENTER); 40 $this->show_all(); 41 } 42 43 public function on_clicked($sender) { 44 45 $md = new GtkMessageDialog($this, Gtk::DIALOG_MODAL, 46 Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, "Download completed."); 47 $md->set_title("Information"); 48 $md->run(); 49 $md->destroy(); 50 } 51 } 52 53 new Example(); 54 Gtk::main();
效果:
先搬运这些例子,这只是原文章中我觉得常用一部分,原文里还有其他的例子。大家有需要可以去看看。
再发一遍原文章地址:http://zetcode.com/gui/phpgtktutorial/
附上文档的类目录:http://gtk.php.net/manual/en/html/gtkclasses.html
下面是自己总结的一部分:
1 1、GtkWindow::set_position(GTK::WIN_POS_CENTER); //window窗口居中 2 2、GtkWindow::set_title('Simple'); //设置窗口标题 3 3、GtkWindow::set_default_size(250,150); //设置默认窗口大小 4 4、GtkWindow::connect_simple('destroy', array('gtk', 'main_quit')); //设置销毁关闭桌面 5 6 7 8 5、 $fixed = new GtkFixed(); //定位容器 9 $this->add($fixed); 10 11 $button = new GtkButton("Button"); //实例化按钮(按钮名) 12 $button->set_size_request(80, 35); //按钮大小 13 $button->set_tooltip_text("Button widget"); //按钮悬浮 14 15 $fixed->put($button, 30, 50); //定位位置 16 17 $this->set_tooltip_text("Window widget"); //空白处悬浮 18 19 6、 GtkImage::new_from_file("mincol.jpg"); //引入图片 20 21 7、GtkWindow::modify_bg(Gtk::STATE_NORMAL, new GdkColor(6400, 6400, 6440)); // 设置背景色 22 23 8、$vbox = new GtkVBox(false, 5); //创建一个垂直盒子容器。false,这意味着放置在垂直框中的小部件将具有相同的大小。小部件之间的垂直间距设置为5像素。 24 25 9、$hbox = new GtkHBox(true,3); //水平盒子容器 盒子内的所有小部件将具有相同的大小。水平部件之间将有3px的空间 26 27 10、new GtkAlignment(1,0,0,0) //创建一个对齐容器,将其子部件放置到右侧。设置为1.0成员会把所有的空闲空间水平框的左侧 28 29 11、$cb = new GtkCheckButton("Show title"); 30 $cb->set_active(true); //单选框 默认选中 31 get_active(); //获取是否选中的状态 32 12、$entry = new GtkEntry(); 33 $entry->connect('key_release_event', array($this, 'on_key_release')); 34 //文字输入框 绑定键盘操作事件 35 $sender->get_text() 获取文本框的内容 36 13、$cb = GtkComboBox::new_text(); //实例化多选框 37 $cb->connect('changed', array($this, 'on_changed')); //绑定选项改变事件 38 39 $cb->append_text('Ubuntu'); 40 $cb->append_text('Mandriva'); 41 $cb->append_text('Redhat'); 42 $cb->append_text('Gentoo'); 43 $cb->append_text('Mint'); //添加选项框项目 44 45 $sender->get_active_text(); //获取被选中的项目 46 47 14、$mb = new GtkMenuBar(); //实例化菜单栏 48 49 $filemenu = new GtkMenu(); //实例化一个菜单 50 $filemi = new GtkMenuItem("File"); //实例化项目 51 $filemi->set_submenu($filemenu); //项目关联一个菜单,加入分菜单 52 53 $exitmi = new GtkMenuItem("Exit"); 54 $exitmi->connect_simple('activate', array('gtk', 'main_quit')); //项目绑定事件 55 $filemenu->append($exitmi); //菜单里插入一个项目 56 57 $mb->append($filemi); //菜单栏里添加拼装好的项目 58 59 注意用GtkMenuItem去set_submenu GtkMenu,项目去包括菜单 60 然后用GtkMenu去append GtkMenuItem 菜单去包含项目 61 用GtkMenuBar去append GtkMenuIte 菜单栏去包含菜单
补充一、关于中文
中文乱码的解决方案,请把自己的php.ini里修改一行
php-gtk.codepage = GBK
并保证代码里的字符集统一
补充二、做刷新需求
如果要做刷新,请注意要先destory掉,然后再次走一边放进容器,并再次show。
尽量层级放的浅一点。
补充三、绑定函数
注意再给按钮绑定函数的时候,被绑定的函数默认接收的第一个参数是绑定的按钮本身,之后的参数才是绑定时的传参
1 $tmpButton->connect('clicked', array($this, 'buttonClick'), $tableName, $buttonName); 2 3 public function buttonClick($button, $tableName, $buttonName){ 4 var_dump($tableName, $buttonName); 5 }