PHP-GTK2-文档学习&翻译——教程部分Hello World (advanced)
张成
2015/12
在这里我们创建一个登陆界面,类似表单。这里有两个输入框,用户名和密码,点击登陆,检验密码正确性,不正确弹出错误框。取消按钮点击退出程序。
运行界面类似如下
首先我们创建一个窗口,设置标题,告诉窗口的关闭函数。
//Create the login window $wnd = new GtkWindow(); $wnd->set_title('Login'); //Close the main loop when the window is destroyed $wnd->connect_simple('destroy', array('gtk', 'main_quit'));
在这个对话框中,创建我们需要的组件,这里我们创建需要的文本输入字段,用户名和密码的输入框组件
$txtUsername = new GtkEntry(); $txtPassword = new GtkEntry();
接下来用户需要知道哪个输入框是代表什么意思的,我们就创建2个文本来标识,哪个是输入用户名和密码。
$lblUsername = new GtkLabel('_Username', true); $lblPassword = new GtkLabel('_Password', true);
最后我们需要2个按钮,一个用来登陆一个取消
$btnLogin = new GtkButton('_Login'); $btnCancel = new GtkButton('_Cancel');
button组件构造函数能自动识别第一个下划线 label组件需要传入第2个参数。
文本需要知道他们被激活时,激活哪个输入框,需要设置 如下 意思是按下ALT+U 激活用户名输入框 按下ALT+P时激活密码框, 上面的label设置下划线 是设置的快捷键功能 ALT加首字母
$lblUsername->set_mnemonic_widget($txtUsername); $lblPassword->set_mnemonic_widget($txtPassword);
下面我们需要注册按钮的事件。
//Destroy the window when the user clicks Cancel $btnCancel->connect_simple('clicked', array($wnd, 'destroy')); //Call the login function when the user clicks on Login $btnLogin->connect_simple('clicked', 'login', $wnd, $txtUsername, $txtPassword);
从第3个参数开始 就是回调函数的参数 将会传入给回调函数
现在我们需要将这些组件加入到窗口中去,但是窗口只能装入一个容器,我们需要一个容器可以装入多个组件,我们使用GTKTable容器,他提供了方法以列排列
//Lay out all the widgets in the table $tbl = new GtkTable(3, 2); $tbl->attach($lblCredit, 0, 2, 0, 1); $tbl->attach($lblUsername, 0, 1, 1, 2); $tbl->attach($txtUsername, 1, 2, 1, 2); $tbl->attach($lblPassword, 0, 1, 2, 3); $tbl->attach($txtPassword, 1, 2, 2, 3);
登陆和取消按钮我们可以放入GtkHButtonBox中去,这个类可放入多个按钮
//Add the buttons to a button box $bbox = new GtkHButtonBox(); $bbox->set_layout(Gtk::BUTTONBOX_EDGE); $bbox->add($btnCancel); $bbox->add($btnLogin);
最后我们需要将 GTKTable 和GTKHButton放入 窗口中去,因为窗口只能放一个组件,所已我们需要把他们放入一个新容器GtkVBox
//Add the table and the button box to a vbox $vbox = new GtkVBox(); $vbox->pack_start($tbl); $vbox->pack_start($bbox);
加入到窗口中
//Add the vbox to the window $wnd->add($vbox); //Show all widgets $wnd->show_all();
login函数检验用户输入,使用GtkEntry的get_text方法获得输入
$strUsername = $txtUsername->get_text(); $strPassword = $txtPassword->get_text();
如果错误发生,我们需要展示一段文字提醒对话框,使用GtkMessageDialog,他自动生成icon和button
$dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $errors); $dialog->set_markup( "The following errors occured:\r\n" . "<span foreground='red'>" . $errors . "</span>" ); $dialog->run(); $dialog->destroy();
我们点击了对话框的YES/NO后 run方法返回 接着销毁自身。
完整的代码如下
<?php /** * Here we create a login window. * It has a username and a password field, and a * Cancel and Login button. Some error checking * is being done when the user clicks "Login". */ if (!class_exists('gtk')) { die("Please load the php-gtk2 module in your php.ini\r\n"); } /** * This function gets called as soon as the user * clicks on the Login button. * * @param GtkWindow $wnd The login window, needed to close it * when all is ok * @param GtkEntry $txtUsername The username text field, used to get * the username * @param GtkEntry $txtPassword The password widget to retrieve the * password */ function login(GtkWindow $wnd, GtkEntry $txtUsername, GtkEntry $txtPassword) { //fetch the values from the widgets into variables $strUsername = $txtUsername->get_text(); $strPassword = $txtPassword->get_text(); //Do some error checking $errors = null; if (strlen($strUsername) == 0) { $errors .= "Username is missing.\r\n"; } if (strlen($strPassword) == 0) { $errors .= "No password given.\r\n"; } if ($errors !== null) { //There was at least one error. //We show a message box with the errors $dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $errors); $dialog->set_markup( "The following errors occured:\r\n" . "<span foreground='red'>" . $errors . "</span>" ); $dialog->run(); $dialog->destroy(); } else { //No error. You would need to hide the dialog now //instead of destroying it (because when you destroy it, //Gtk::main_quit() gets called) and show the main window $wnd->destroy(); } } //Create the login window $wnd = new GtkWindow(); $wnd->set_title('Login'); //Close the main loop when the window is destroyed $wnd->connect_simple('destroy', array('gtk', 'main_quit')); //Set up all the widgets we need $lblCredit = new GtkLabel('Please provide your data'); //The second parameter says that the underscore should be parsed as underline $lblUsername = new GtkLabel('_Username', true); $lblPassword = new GtkLabel('_Password', true); $txtUsername = new GtkEntry(); $txtPassword = new GtkEntry(); $btnLogin = new GtkButton('_Login'); $btnCancel = new GtkButton('_Cancel'); //Which widget should be activated when the // mnemonic (Alt+U or Alt+P) is pressed? $lblUsername->set_mnemonic_widget($txtUsername); $lblPassword->set_mnemonic_widget($txtPassword); //Hide the password //$txtPassword->set_invisible_char('*'); //Destroy the window when the user clicks Cancel $btnCancel->connect_simple('clicked', array($wnd, 'destroy')); //Call the login function when the user clicks on Login $btnLogin->connect_simple('clicked', 'login', $wnd, $txtUsername, $txtPassword); //Lay out all the widgets in the table $tbl = new GtkTable(3, 2); $tbl->attach($lblCredit, 0, 2, 0, 1); $tbl->attach($lblUsername, 0, 1, 1, 2); $tbl->attach($txtUsername, 1, 2, 1, 2); $tbl->attach($lblPassword, 0, 1, 2, 3); $tbl->attach($txtPassword, 1, 2, 2, 3); //Add the buttons to a button box $bbox = new GtkHButtonBox(); $bbox->set_layout(Gtk::BUTTONBOX_EDGE); $bbox->add($btnCancel); $bbox->add($btnLogin); //Add the table and the button box to a vbox $vbox = new GtkVBox(); $vbox->pack_start($tbl); $vbox->pack_start($bbox); //Add the vbox to the window $wnd->add($vbox); //Show all widgets $wnd->show_all(); //Start the main loop Gtk::main(); ?>