- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Widgets in PHP GTK
In this part of the PHP GTK programming tutorial, we will introduce some widgets.
Widgets are basic building blocks of a GUI application. Over the years, several widgets became a standard in all toolkits on all OS platforms. For example a button, a check box or a scroll bar. The GTK toolkit's philosophy is to keep the number of widgets at a minimum level. More specialised widgets are created as custom GTK widgets.
GtkCheckButton
A GtkCheckButton
is a widget that has two states: on and off. The on state is visualised by a check mark. It is used to denote some boolean property.
<?php /* ZetCode PHP GTK tutorial This program toggles the title of the window with the GtkCheckButton widget. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } private function init_ui() { $this->set_title('Check button'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $fixed = new GtkFixed(); $this->add($fixed); $cb = new GtkCheckButton("Show title"); $cb->set_active(true); $cb->connect('clicked', array($this, 'on_clicked')); $fixed->put($cb, 50, 50); $this->set_default_size(250, 200); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } public function on_clicked($sender) { if ($sender->get_active()) { $this->set_title("Check button"); } else { $this->set_title(""); } } } new Example(); Gtk::main(); ?>
We will display a title in the titlebar of the window, depending on the state of the GtkCheckButton
.
$cb = new GtkCheckButton("Show title");
The GtkCheckButton
widget is created. The constructor of the widget takes one parameter, a label. The label is shown next to the check box.
$cb->set_active(true);
The title is visible by default, so we check the check button by default.
$cb->connect('clicked', array($this, 'on_clicked'));
If we click on the check box widget a clicked signal is emitted. We hook on_clicked()
method to the signal.
if ($sender->get_active()) { $this->set_title("Check button"); } else { $this->set_title(""); }
We show the title if the button is checked. The get_active()
method is used to determine the state of the check button. The set_title()
method is used to set the title of the window. To clear the title of the window, we use an empty string.

GtkLabel
The GtkLabel
widget shows text. No user interaction is available with this widget.
<?php /* ZetCode PHP GTK tutorial In this example, we show a text on the window. For this, we use the GtkLabel widget. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } private function init_ui() { // no trailing white space! $lyrics = <<<LYRICS Meet you downstairs in the bar and heard your rolled up sleeves and your skull t-shirt You say why did you do it with him today? and sniff me out like I was Tanqueray cause you're my fella, my guy hand me your stella and fly by the time I'm out the door you tear men down like Roger Moore I cheated myself like I knew I would I told ya, I was trouble you know that I'm no good LYRICS; $this->set_title('GtkLabel'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $this->set_border_width(10); $label = new GtkLabel($lyrics); $this->add($label); $this->set_default_size(250, 200); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } } new Example(); Gtk::main(); ?>
The code example shows some lyrics on the window.
// no trailing white space! $lyrics = <<<LYRICS Meet you downstairs in the bar and heard your rolled up sleeves and your skull t-shirt
We create a multi-line text. In PHP, a multi-line text can be created using the heredoc syntax.
$this->set_border_width(10);
The GtkLabel
is surrounded by some empty space.
$label = new GtkLabel($lyrics); $this->add($label);
The GtkLabel
widget is created and added to the window.

GtkEntry
The GtkEntry
is a single line text entry field. This widget is used to enter textual data.
<?php /* ZetCode PHP GTK tutorial This example demonstrates the GtkEntry widget. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { private $label; public function __construct() { parent::__construct(); $this->init_ui(); } private function init_ui() { $this->set_title('GtkEntry'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $fixed = new GtkFixed(); $this->label = new GtkLabel("..."); $fixed->put($this->label, 60, 40); $entry = new GtkEntry(); $fixed->put($entry, 60, 100); $entry->connect('key_release_event', array($this, 'on_key_release')); $this->add($fixed); $this->set_default_size(250, 200); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } public function on_key_release($sender, $event) { $this->label->set_text($sender->get_text()); } } new Example(); Gtk::main(); ?>
This example shows an entry widget and a label. The text that we key in the entry is displayed immediately in the label widget.
$entry = new GtkEntry();
GtkEntry
widget is created.
$entry->connect('key_release_event', array($this, 'on_key_release'));
We plug the the on_key_release() method to the key_release_event
of the GtkEntry
widget.
public function on_key_release($sender, $event) { $this->label->set_text($sender->get_text()); }
Inside the method, we get the text from the GtkEntry
widget via the get_text()
method and set it to the label using the set_text()
method of the label.

GtkImage
The GtkImage
widget shows an image.
<?php /* ZetCode PHP GTK tutorial This example demonstrates the GtkImage widget. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } private function init_ui() { $this->set_title('Red Rock'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $this->set_border_width(2); $image = GtkImage::new_from_file("redrock.png"); $this->add($image); $this->set_default_size(250, 200); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } } new Example(); Gtk::main(); ?>
In our example, we show an image on the window.
$this->set_border_width(2);
We put some empty border around the image.
$image = GtkImage::new_from_file("redrock.png");
The GtkImage
widget is created. We load the image from the file using the static new_from_file()
method. If the file isn't found or can't be loaded, the resulting GtkImage
will display a broken image icon.
$this->add($image);
Widget is added to the container.
GtkComboBox
A ComboBox
is a widget that allows the user to choose from a list of options.
<?php /* ZetCode PHP GTK tutorial This example demonstrates the GtkComboBox widget author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { private $label; public function __construct() { parent::__construct(); $this->init_ui(); } private function init_ui() { $this->set_title('GtkComboBox'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $fixed = new GtkFixed(); $this->label = new GtkLabel('-'); $fixed->put($this->label, 50, 140); $cb = GtkComboBox::new_text(); $cb->connect('changed', array($this, 'on_changed')); $cb->append_text('Ubuntu'); $cb->append_text('Mandriva'); $cb->append_text('Redhat'); $cb->append_text('Gentoo'); $cb->append_text('Mint'); $fixed->put($cb, 50, 30); $this->add($fixed); $this->set_default_size(250, 200); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } public function on_changed($sender) { $this->label->set_label($sender->get_active_text()); } } new Example(); Gtk::main(); ?>
The example shows a combo box and a label. The combo box has a list of five options. These are the names of Linux distros. The label widget shows the selected option from the combo box.
$cb = GtkComboBox::new_text();
The GtkComboBox
widget is created. The new_text()
is a method that creates a GtkComboBox
just displaying strings.
$cb->append_text('Ubuntu'); $cb->append_text('Mandriva'); $cb->append_text('Redhat'); $cb->append_text('Gentoo'); $cb->append_text('Mint');
It is filled with data.
public function on_changed($sender) { $this->label->set_label($sender->get_active_text()); }
Inside the on_changed()
method, we get the selected text out of the combo box and set it to the label.

In this chapter of the PHP GTK tutorial, we showed some basic widgets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论