发新话题
打印

PHPGTK的常用控件以及信号处理

本主题由 phpres 于 2007-6-19 11:20 加入精华

PHPGTK的常用控件以及信号处理

本章节要介绍PHPGTK的常用控件以及信号处理,以便让大家逐渐熟悉PHPGTK2的程序
GTK基础对以后的程序开发是很重要的

     所谓窗体布局,就是要设计窗体上某个部件在窗体上哪个位置,看似简单的,其实并不是很容易理解
本节将介绍窗体装入格状控件、GtkHBox 横向布局控件、GtkVBox 纵向布局控件、GtkTable 表格布局控件

1、GtkHBox 横向布局控件
复制内容到剪贴板
代码:
<?
// test.php
function quit()
{
    Gtk::main_quit();
}

$window = new GtkWindow();
$window->set_default_size(260,100);
$window->set_title("GTK HBOX");

$window->connect("destroy","quit");

$button1 = new GtkButton("button1");
$button2 = new GtkButton("button2");

$hbox = new GtkHBox(True,1);    // 创建一个HBox对象 均匀填充为True ,和其他容器的间距为1
$hbox->pack_start($button1,true,true);    // 把button1装入到HBox里
$hbox->pack_start($button2,true,true);    // 把button2装入到HBox里
                        /*
                        GtkHBox pack_start函数原形:
                        void pack_start(GtkWidget child [, bool expand = true [, bool fill = true [, int padding = 0]]]);
                        */

$window->add($hbox);


$window->show_all();    // 显示窗体
Gtk::main();    //进入GTK主循环
?>
运行结果如下图




http://www.phpgtk.net/  PHP GTK2 中文教程

TOP

2、GtkVBox 纵向布局控件
复制内容到剪贴板
代码:
<?
// test.php
function quit()
{
    Gtk::main_quit();
}

$window = new GtkWindow();
$window->set_default_size(260,100);
$window->set_title("GTK VBOX");

$window->connect("destroy","quit");

$button1 = new GtkButton("button1");
$button2 = new GtkButton("button2");

$vbox = new GtkVBox(True,1);    // 创建一个VBox对象 均匀填充为True ,和其他容器的间距为1
$vbox->pack_start($button1,true,true);    // 把button1装入到HBox里
$vbox->pack_start($button2,true,true);    // 把button2装入到HBox里
                        /*
                        GtkVBox pack_start函数原形:
                        void pack_start(GtkWidget child [, bool expand = true [, bool fill = true [, int padding = 0]]]);
                        */

$window->add($vbox);    // 把vbox加到窗体上


$window->show_all();    // 显示窗体
Gtk::main();    //进入GTK主循环
?>
运行结果如下图




http://www.phpgtk.net/  PHP GTK2 中文教程

TOP

3、GtkTable 表格布局控件
复制内容到剪贴板
代码:
<?
$w = new GtkWindow();
$w->set_title('GtkTable test');
$w->connect_simple('destroy', array('gtk', 'main_quit'));

$lbl1   = new GtkLabel('Email address:');
$lbl2   = new GtkLabel('Id:');
$lbl3   = new GtkLabel('Name:');
$align3 = new GtkAlignment(0.0, 0.5, 0, 0);
$align3->add($lbl3);
$txt1   = new GtkEntry();
$txt2   = new GtkEntry();
$txt3   = new GtkEntry();

$table  = new GtkTable(3, 2);        // 创建一个3行2列的表格布局控件
$table->attach($lbl1  , 0, 1, 0, 1, 0);    // 把lbl1装入到表格的第一行第一列中
/*
attach的函数原形
void attach(GtkWidget child,
    int left_attach,
    int right_attach,
    int top_attach,
    int bottom_attach [,
    GtkAttachOptions xoptions = Gtk::EXPAND|Gtk::FILL [,      
    GtkAttachOptions yoptions = Gtk::EXPAND|Gtk::FILL [,
    int xpadding = Gtk::EXPAND|Gtk::FILL [,
    int ypadding = Gtk::EXPAND|Gtk::FILL]]]]);
0,1,0,1,0 的意思就是说 lbl1所占用的空间是这样的
0-----------------1---------------------2
| lbl1            |                     |
|                 |                     |
1---------------------------------------|
|                 |                     |
|                 |                     |
2---------------------------------------|
|                 |                     |
|                 |                     |
3---------------------------------------|
*/

$table->attach($lbl2  , 0, 1, 1, 2, 0);
$table->attach($align3, 0, 1, 2, 3, Gtk::FILL);
$table->attach($txt1  , 1, 2, 0, 1);
$table->attach($txt2  , 1, 2, 1, 2);
$table->attach($txt3  , 1, 2, 2, 3);



$w->add($table);
$w->show_all();
Gtk::main();

?>
经常有人说布局很麻烦,其实不是这样的,熟能生巧,多用多练,自然就用的熟练了
http://www.phpgtk.net/  PHP GTK2 中文教程

TOP

发新话题