So far, you have had an in-depth view of everything that you can do with GTK+ and associated technologies. In this chapter, we’re going to put this knowledge to work by building a few applications.
This chapter introduces five full applications: the file browser that was designed in Chapter 11, a calculator, a ping utility, a hangman game, and a calendar. However, the source code for the examples is not contained in this chapter. The code for each of the applications in this chapter can be downloaded from www.gtkbook.com .
I will conclude this final chapter of the book by offering pointers to other learning resources so that you can continue expanding your GTK+ knowledge.
File Browser
In Chapter 11, you implemented the user interface of a file browser application in Glade. The user interface was dynamically loaded, and all the signals were autoconnected with Gtk.Builder.
The file browsing capabilities are of special interest in this application. They are very similar to those in Chapter 9’s “Exercise 1: File Browser” section. In that exercise, you created a simple application using a Gtk.TreeView widget that could browse the user’s file system. The current location of the file browser is stored in a linked list from which the full path can be built. Each node in the list is one part of the path, and the directory separator is placed between each string to build the full path. A Gtk.Entry widget is also provided to allow the user to edit the path with the keyboard.
Navigation through the file system can be done using a few different methods. The location can be entered in the address bar, although the validity of the location must be verified when the Gtk.Entry widget is activated. In addition to this method, the user can use the Back, Forward, Up, or Home toolbar buttons to navigate through the browsing history, move to the parent directory, or go to the home directory, respectively. Lastly, Gtk.TreeView’s row-activated signal allows the user to move into the selected directory or view information about the selected file.
A Gtk.StatusBar widget is placed along the bottom of the window. It keeps track of the total number of items in the current directory and the total size of these items. The sources for this example, along with the four other applications in this chapter, can be downloaded from www.gtkbook.com .
Calculator
This calculator application was designed in Glade, so the user interface was completed with absolutely no code. Since most of the widgets in this example are Gtk.Button widgets, the clicked and destroy signals were the only two needed.
The calculator allows the user to enter numbers with an optional decimal point, perform four basic operations (add, subtract, multiply, and divide), negate numbers, and calculate square roots and exponents. To cut down on the number of callback methods needed, all the numbers and the decimal place were connected to a single callback method called num_clicked(), and the four basic operations and the power operations were connected to one another. This allows you to take advantage of the fact that these groups of operations need a lot of similar code to work.
When a number or the decimal point button is clicked, the character is appended to the end of the current value, although the length of the number is restricted to ten digits. When an operation button is clicked, the operation is performed, and the new value is stored. It also sets a flag called clear_flag that tells the calculator that a new number should be started when the user presses a number or decimal place.
Ping Utility
In this application, the GLib spawn_async_with_pipes() function is used to fork an instance of the ping application with the specified address. The shell command received by this function was parsed with the shell_parse_argv() function so that it was in the correct format. The Ping button is disabled, which prevents the user from running multiple instances of the child process.
After spawning the child process, the output pipe is used to create a new Channel object that watches the pipe for read data. When data is ready to be read, it is parsed so that statistics for each ping can be displayed in a Gtk.TreeView widget. This continues for the specified number of times or until the user stops the child.
When a child process is running, a Stop button is enabled, which allows the user to kill the child process before it completes. This function simply calls the following instance of the os.killpg() function, which forces the child process to close.
When the process is killed, the pipe is destroyed, which causes the channel to shut down in the watch function. This ensures that we are able to reuse the same Channel object for the next child process.
Calendar
Calendar File
A new calendar is created by clicking the New toolbar button, which asks for a calendar file name and location. The calendar is saved every time you add or remove an event, so a Save button is not provided. You can also open an existing calendar by pressing the Open toolbar button.
Markup Parser Functions
To open a calendar, this application uses XML-SAX’s parser to retrieve the contents of the file. This parser is very easy to use and supports basic XML files. The first thing you need to do to use the parser is define a new xmlparser object. This object has many attributes, including four user-defined functions that you need to code yourself; I cover them one at a time. Any of these functions can be set to None.
Note
The CharacterData() method is not only called for tags that contain strings but also for tags that call other tags; therefore, this function may have a text parameter filled with spaces and new line characters!
Parsing the XML File
This function creates and returns a new xmlparser object.
XML-SAX can also do XML namespace processing for you. See the documentation for more information.
Further Resources
Congratulations! You have now completed reading this book, and you know enough to develop and manage complex GTK+ applications. However, you may be wondering where you should go from here. There are a number of libraries and resources that will become indispensable as you begin developing applications on your own.
The first resource is the book’s web site ( www.gtkbook.com ). This site includes links to online resources for GTK + developers, as well as tutorials on topics that did not fit in this book. You can use it as a starting point for finding help with GTK+ application development.
Another great resource is the GTK+ web site ( www.gtk.org ). This site includes information about mailing lists, downloads, and bug tracking for GTK+. You can find up-to-date documentation on this site as well.
The PyGObject API Reference ( http://lazka.github.io/pgi-docs ) is a one-stop web site for all things related to Python, GNOME, GTK+, ATK, GDK, and many other libraries.
The Pycairo API Reference ( http://pycairo.readthedocs.io/en/latest/reference/index.html ) has documentation for all the Python APIs for Cairo.
The Python web site ( www.python.org ) has documentation for all versions of Python 2.x and 3.x. It includes references, tutorials, how-to’s, FAQs, PyPi, and information about the Python Software Foundation.
Summary
You have become familiar with a large portion of GTK+ and its supporting libraries. This knowledge can be used to implement graphical user interfaces for applications on many platforms.
This book is intended to give you a thorough understanding of GTK+, and I hope that it will continue to be a valuable resource as you develop applications. The appendixes are indispensable references for topics that are not always thoroughly documented in the API documentation; they can be used even when you become an expert. The last appendix provides short descriptions of exercise solutions and tips on how to complete them.
Now that you have this knowledge, practice and experience will help you become a great graphical application developer. You have everything you need to continue on your own. I hope you have had as much fun reading this book as I have had writing it!