Password Entry Fields
Make a GtkEntry mask contents typed into it.
Topic: GtkEntry (View All Tutorials)
Keywords: gtkentry password masking
Updated: 363 Days Ago, 2007/12/04 23:54
It is quite common in data entry forms such as a login form, to make the password field display a masking character instead of showing the user's actual password. GTK makes this rather simple, it requires only one method to toggle on the input mask.
Note
This does not actually encrypt the password or anything, it is just a mask to keep the nosy guy in the office who is always breathing down your neck from watching over your shoulder and stealing your login details.
Small Login Form
#!/usr/bin/php -c/etc/gtk/php.ini
<?php
class bobLoginDialog extends GtkWindow {
public $vbox,$bbox;
public $usertext,$passtext;
public $username,$password;
public $submit;
public function __construct() {
parent::__construct();
$this->vbox = new GtkVBox;
$this->bbox = new GtkHBox;
$this->username = new GtkEntry;
$this->password = new GtkEntry;
$this->usertext = new GtkAlignment(0,0,0,0);
$this->passtext = new GtkAlignment(0,0,0,0);
$this->submit = new GtkButton('Log In');
$this->usertext->add(new GtkLabel('User Name:'));
$this->passtext->add(new GtkLabel('Password:'));
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_title('Login Dialog');
$this->set_border_width(4);
$this->set_resizable(false);
// note 1
$this->password->set_visibility(false);
$this->connect_simple('delete-event',array($this,'on_quit'));
$this->submit->connect_simple('clicked',array($this,'on_quit'));
$this->bbox->pack_end($this->submit,false,false,0);
$this->vbox->pack_start($this->usertext,false,false,3);
$this->vbox->pack_start($this->username,false,false,3);
$this->vbox->pack_start($this->passtext,false,false,3);
$this->vbox->pack_start($this->password,false,false,3);
$this->vbox->pack_start($this->bbox,false,false,0);
$this->add($this->vbox);
$this->show_all();
return;
}
public function on_quit() {
$this->hide();
Gtk::main_quit();
return;
}
}
$w = new bobLoginDialog;
Gtk::main();
$w->destroy();
unset($w);
exit(0);
?>
Note 1
This is it, the set_visibility() method of GtkEntry. It takes one argument, false to enable the input masking, true to make it show the actual data. If that sounds backwards, think of it as "can I see the data? false, I see asterisks instead."
End Game