繼續學習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 }
