© W. David Ashley and Andrew Krause 2019
W. David Ashley and Andrew KrauseFoundations of PyGTK Developmenthttps://doi.org/10.1007/978-1-4842-4179-0_14

14. Integrating Everything

W. David Ashley1  and Andrew Krause2
(1)
AUSTIN, TX, USA
(2)
Leesburg, VA, USA
 

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.

At the end of Chapter 11, you were told that the callback methods would be implemented in this chapter, and we will do so now. Figure 14-1 shows the file browser application when it is first launched. It is displaying the root folder.
../images/142357_2_En_14_Chapter/142357_2_En_14_Fig1_HTML.jpg
Figure 14-1

The file browser using Gtk.TreeView

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

A calculator is a simple application that is implemented in most GUI programming books. This example is meant to show you just how easy it is to implement a calculator. Figure 14-2 is a screenshot of the application.
../images/142357_2_En_14_Chapter/142357_2_En_14_Fig2_HTML.jpg
Figure 14-2

A simple calculator application

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 program, you learn how to use channels in the GLib library to communicate with applications through pipes. A ping utility application is displayed in Figure 14-3; it allows the user to ping an address a specific number of times or continually until the application is stopped.
../images/142357_2_En_14_Chapter/142357_2_En_14_Fig3_HTML.jpg
Figure 14-3

A ping utility application

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

The last application in this chapter creates a calendar that organizes events for the user. It uses the Gtk.Calendar widget to allow the user to browse dates. Gtk.TreeView displays events on the given day. Figure 14-4 shows this calendar application.
../images/142357_2_En_14_Chapter/142357_2_En_14_Fig4_HTML.jpg
Figure 14-4

A calendar application with two events

Most of the code to create the calendar application should look very familiar, because it uses functions introduced in previous chapters. In addition to the familiar functions, the application uses the XML parser provided by XML-SAX to open calendar files, which are stored as XML files. An example calendar file that contains one event is shown in Listing 14-1.
<calendar>
  <event>
    <name>Release of the Book</name>
    <location>Everywhere</location>
    <day>16</day>
    <month>3</month>
    <year>2007</year>
    <start>All Day</start>
    <end></end>
  </event>
</calendar>
Listing 14-1

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.

The first method, StartElement(), is called for every open tag, such as <calendar> and <event>. This function receives the name of the tag element along with arrays of attribute names and values. This allows you to differentiate between starting elements, checking for attributes when appropriate. In the calendar application, this function is used to free all the temporary data stored for the previous event, creating a clean slate for the next event.
StartElement(name, attributes)
The next method, EndElement(), is called for every close tag, such as </calendar> and </event>. It is also called for tags that have no close tag, such as <tag/>. Similar to the previous method, it accepts the tag name. In the calendar application, it is used to add the event to the global tree if the </event> tag has been reached.
EndElement(name)
The CharacterData() method is called for the data found between StartElement() and EndElement() calls. It accepts the text between the two tags as well as the length of the text. This function is called in the calendar application to read the content of an event.
CharacterData(data)

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

The parsing of the XML text is done with an xmlparser object. You can create a new parser with xml.sax.parse(filename, contenthandler):
xml.sax.parse(filename, contenthandler))

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 GNOME developer’s web site ( http://developer.gnome.org ) is also an ideal place to learn more. In addition to GTK+ and its supporting libraries, there are a number of other libraries used to develop applications for GNOME that you will continually run across. The following list briefly summarizes a few of these libraries.

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!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.181