- 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
Layout management in PHP GTK
In this chapter we will show how to lay out our widgets in windows or dialogs.
When we design the GUI of our application, we decide what widgets we will use and how we will organise those widgets in the application. To organise our widgets, we use specialised non visible widgets called layout containers. In this chapter, we will mention GtkAlignment
, GtkFixed
, GtkVBox
and GtkTable
.
GtkFixed
The GtkFixed
container places child widgets at fixed positions and with fixed sizes. This container performs no automatic layout management. In most applications, we do not use this container. There are some specialised areas, where we use it. For example games, specialised applications that work with diagrams, resizable components that can be moved (like a chart in a spreadsheet application), small educational examples.
<?php /* ZetCode PHP GTK tutorial In this program, we lay out widgets using absolute positioning. author: Jan Bodnar website: www.zetcode.com last modified: September 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } public function init_ui() { $this->set_title('Fixed'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $this->modify_bg(Gtk::STATE_NORMAL, new GdkColor(6400, 6400, 6440)); $bardejov = GtkImage::new_from_file("bardejov.jpg"); $rotunda = GtkImage::new_from_file("rotunda.jpg"); $mincol = GtkImage::new_from_file("mincol.jpg"); $fixed = new GtkFixed(); $fixed->put($bardejov, 20, 20); $fixed->put($rotunda, 40, 160); $fixed->put($mincol, 170, 50); $this->add($fixed); $this->set_default_size(300, 280); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } } new Example(); Gtk::main(); ?>
In our example, we show three small images on the window. We explicitly specify the x, y coordinates, where we place these images.
$this->modify_bg(Gtk::STATE_NORMAL, new GdkColor(6400, 6400, 6440));
For better visual experience, we change the background color to dark gray.
$bardejov = GtkImage::new_from_file("bardejov.jpg");
The GtkImage
is a widget that is used to display images. The picture is loaded from the file on the disk.
$fixed = new GtkFixed();
We create the GtkFixed
container.
$fixed->put($bardejov, 20, 20);
We place the first image at x=20, y=20 coordinates.
w.add(fixed);
Finally, we add the GtkFixed
container to the Window.

Buttons
In this code example, we will use a vertical box, a horizontal box, and an alignment widget. A horizontal box arranges widgets in a single row. Similarly, a vertical box places its widgets in one column. The Alignment
container controls the alignment and the size of its child widget.
<?php /* ZetCode PHP GTK tutorial In this program, we position two buttons in the bottom right corner of the window. We use horizontal and vertical boxes. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } public function init_ui() { $this->set_title('Buttons'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $this->set_border_width(3); $vbox = new GtkVBox(false, 5); $hbox = new GtkHBox(true, 3); $frame = new GtkFrame(); $vbox->pack_start($frame, true, true, 0); $okButton = new GtkButton("OK"); $okButton->set_size_request(70, 30); $closeButton = new GtkButton("Close"); $hbox->add($okButton); $hbox->add($closeButton); $halign = new GtkAlignment(1, 0, 0, 0); $halign->add($hbox); $vbox->pack_start($halign, false, false, 3); $this->add($vbox); $this->set_default_size(260, 150); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } } new Example(); Gtk::main(); ?>
In the code example, we place two buttons into the right bottom corner of the window. To accomplish this, we use one horizontal box and one vertical box and one alignment container.
$vbox = new GtkVBox(false, 5);
A vertical box container is created. We set the homogeneous member to false. This means that widgets put into the vertical box will hot have the same size. The vertical spacing between widgets is set to 5 pixels.
$frame = new GtkFrame();
Here we create a GtkFrame
widget. The purpose of this widget is to take up space above the two buttons.
$vbox->pack_start($frame, true, true, 0);
Here we place the frame widget into the vertical box. The first parameter of the method is the widget, which is being placed into the box. The following three parameters are expand, fill and padding. The expand parameter is set to true, which means that free space will be allocated around the widget. When the fill parameter is set to true, the widget actually takes up all the free space around it. There is no padding around the child widget.
$hbox = new GtkHBox(true, 3);
This code line creates a horizontal box. All widgets inside the box will have the same size. There will be 3px space between the widgets horizontally.
$okButton = new GtkButton("OK"); $okButton->set_size_request(70, 30); $closeButton = new GtkButton("Close"); $hbox->add($okButton); $hbox->add($closeButton);
We create two buttons and put them inside the horizontal box.
$halign = new GtkAlignment(1, 0, 0, 0); $halign->add($hbox); $vbox->pack_start($halign, false, false, 3);
This will create an alignment container that will place its child widget to the right. The xalign
member set to 1.0 will put all free space to the left of the horizontal box. This will push the two buttons to the right. We add the horizontal box into the alignment container and pack the alignment container into the vertical box. We must keep in mind that the alignment container takes only one child widget. That's why we must use the horizontal box.

Calculator skeleton
The GtkTable
widget arranges widgets in rows and columns.
<?php /* ZetCode PHP GTK tutorial In this program we create a skeleton of a calculator. We use the GtkTable widget. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } public function init_ui() { $this->set_title('Calculator'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $vbox = new GtkVBox(false, 2); $mb = new GtkMenubar(); $filemenu = new GtkMenu(); $filemi = new GtkMenuItem("File"); $filemi->set_submenu($filemenu); $mb->append($filemi); $vbox->pack_start($mb, false, false, 0); $table = new GtkTable(5, 4, true); $table->attach_defaults(new GtkButton("Cls"), 0, 1, 0, 1); $table->attach_defaults(new GtkButton("Bck"), 1, 2, 0, 1); $table->attach_defaults(new GtkLabel(), 2, 3, 0, 1); $table->attach_defaults(new GtkButton("Close"), 3, 4, 0, 1); $table->attach_defaults(new GtkButton("7"), 0, 1, 1, 2); $table->attach_defaults(new GtkButton("8"), 1, 2, 1, 2); $table->attach_defaults(new GtkButton("9"), 2, 3, 1, 2); $table->attach_defaults(new GtkButton("/"), 3, 4, 1, 2); $table->attach_defaults(new GtkButton("4"), 0, 1, 2, 3); $table->attach_defaults(new GtkButton("5"), 1, 2, 2, 3); $table->attach_defaults(new GtkButton("6"), 2, 3, 2, 3); $table->attach_defaults(new GtkButton("*"), 3, 4, 2, 3); $table->attach_defaults(new GtkButton("1"), 0, 1, 3, 4); $table->attach_defaults(new GtkButton("2"), 1, 2, 3, 4); $table->attach_defaults(new GtkButton("3"), 2, 3, 3, 4); $table->attach_defaults(new GtkButton("-"), 3, 4, 3, 4); $table->attach_defaults(new GtkButton("0"), 0, 1, 4, 5); $table->attach_defaults(new GtkButton("."), 1, 2, 4, 5); $table->attach_defaults(new GtkButton("="), 2, 3, 4, 5); $table->attach_defaults(new GtkButton("+"), 3, 4, 4, 5); $vbox->pack_start(new GtkEntry(), false, false, 0); $vbox->pack_end($table, true, true, 0); $this->add($vbox); $this->set_default_size(300, 250); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } } new Example(); Gtk::main(); ?>
We use the GtkTable
widget to create a calculator skeleton.
$table = new GtkTable(5, 4, true);
We create a table widget with 5 rows and 4 columns. The third parameter is the homogenous parameter. If set to true, all the widgets in the table are of the same size. The size of all widgets is equal to the largest widget in the table container.
$table->attach_defaults(new GtkButton("Cls"), 0, 1, 0, 1);
We attach a button to the table container. To the top-left cell of the table. The first two parameters are the left and right sides of the cell, the last two parameters are the top and left sides of the cell.
$vbox->pack_end($table, true, true, 0);
We pack the table widget into the vertical box.

Windows
Next we will create a more advanced example. We show a window, that can be found in the JDeveloper IDE.
<?php /* ZetCode PHP GTK tutorial This is a more complicated layout example. We use GtkAlignment and GtkTable widgets. author: Jan Bodnar website: www.zetcode.com last modified: August 2011 */ class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } public function init_ui() { $this->set_title('Windows'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $this->set_border_width(15); $table = new GtkTable(8, 4, false); $table->set_col_spacings(3); $title = new GtkLabel("Windows"); $halign = new GtkAlignment(0, 0, 0, 0); $halign->add($title); $table->attach($halign, 0, 1, 0, 1, GTK::FILL, GTK::FILL, 0, 0); $frame = new GtkFrame(); $table->attach($frame, 0, 2, 1, 3, GTK::FILL | Gtk::EXPAND, GTK::FILL | GTK::EXPAND, 1, 1); $activate = new GtkButton("Activate"); $activate->set_size_request(50, 30); $table->attach($activate, 3, 4, 1, 2, GTK::FILL, GTK::SHRINK, 1, 1); $valign = new GtkAlignment(0, 0, 0, 0); $close = new GtkButton("Close"); $close->set_size_request(70, 30); $valign->add($close); $table->set_row_spacing(1, 3); $table->attach($valign, 3, 4, 2, 3, Gtk::FILL, Gtk::FILL | Gtk::EXPAND, 1, 1); $halign2 = new GtkAlignment(0, 1, 0, 0); $help = new GtkButton("Help"); $help->set_size_request(70, 30); $halign2->add($help); $table->set_row_spacing(3, 6); $table->attach($halign2, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL, 0, 0); $ok = new GtkButton("OK"); $ok->set_size_request(70, 30); $table->attach($ok, 3, 4, 4, 5, Gtk::FILL, Gtk::FILL, 0, 0); $this->add($table); $this->set_default_size(300, 250); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } } new Example(); Gtk::main(); ?>
The code example shows, how we can create a similar window in PHP GTK.
$table = new GtkTable(8, 4, false); $table->set_col_spacings(3);
The example is based on the GtkTable
container. There will be 3px space between columns.
$title = new GtkLabel("Windows"); $halign = new GtkAlignment(0, 0, 0, 0); $halign->add($title); $table->attach($halign, 0, 1, 0, 1, GTK::FILL, GTK::FILL, 0, 0);
This code creates a label that is aligned to the left. The label is placed in the first row of the first column of the Table container.
$frame = new GtkFrame(); $table->attach($frame, 0, 2, 1, 3, GTK::FILL | Gtk::EXPAND, GTK::FILL | GTK::EXPAND, 1, 1);
The frame widget spans two rows and two columns. It will consume all free space around it. Thus, taking the bulk of the area of the window.
$valign = new GtkAlignment(0, 0, 0, 0); $close = new GtkButton("Close"); $close->set_size_request(70, 30); $valign->add($close); $table->set_row_spacing(1, 3); $table->attach($valign, 3, 4, 2, 3, Gtk::FILL, Gtk::FILL | Gtk::EXPAND, 1, 1);
We put the close button next to the frame widget into the fourth column. (we count from zero) We add the button into the alignment widget, so that we can align it to the top.
$halign2 = new GtkAlignment(0, 1, 0, 0); $help = new GtkButton("Help"); $help->set_size_request(70, 30); $halign2->add($help); $table->set_row_spacing(3, 6); $table->attach($halign2, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL, 0, 0);
The help button is placed into the alignment widget, so that it can be left aligned in its table cell. It is placed at the first column, fifth row.
$ok = new GtkButton("OK"); $ok->set_size_request(70, 30); $table->attach($ok, 3, 4, 4, 5, Gtk::FILL, Gtk::FILL, 0, 0);
Finally the ok button. It is placed at the fourth column and fifth row.

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

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