W. David Ashley and Andrew Krause
Foundations of PyGTK DevelopmentGUI Creation with Python2nd ed.
W. David Ashley
AUSTIN, TX, USA
Andrew Krause
Leesburg, VA, USA
ISBN 978-1-4842-4178-3e-ISBN 978-1-4842-4179-0
Library of Congress Control Number: 2018966864
© W. David Ashley and Andrew Krause 2019
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

I dedicate this book to my wife.

Without you, all of this would not be possible.

—W. David Ashley

Introduction

One of the most important aspects of an application is the interface that is provided to interact with the user. With the unprecedented popularity of computers in society today, people have come to expect those user interfaces to be graphical, and the question of which graphical toolkit to use quickly arises for any developer. For many, the cross-platform, feature-rich GTK+ library is the obvious choice.

Learning GTK+ can be a daunting task, because many features lack documentation and others are difficult to understand even with the API documentation. Foundations of PyGTK Development aims to decrease the learning curve and set you on your way to creating cross-platform graphical user interfaces for your applications.

Each chapter in this book contains multiple examples that help you further your understanding. In addition to these examples, the final chapter of this book provides five complete applications that incorporate topics from the previous chapters. These applications show you how to bring together what you have learned to accomplish in various projects.

Each chapter starts with an overview, so that you are able to skip around if you want. Most chapters also contain exercises to test your understanding of the material. I recommend that you complete all the exercises before continuing, because the best way to learn GTK+ is to use it.

At the end of this book, there are multiple appendixes that serve as references for various aspects of GTK+. These appendixes include tables listing signals, styles, and properties for every widget in GTK+. These appendixes will remain a useful reference after you have finished reading the book and begin creating your own applications. In addition, Appendix D explains the solutions to all the exercises in the book.

Who Should Read This Book

Because this book begins with the basics and works up to more difficult concepts, you do not need any previous knowledge of GTK+ development to use this book. This book does assume that you have a decent grasp of the Python programming language. You should also be comfortable with running commands and terminating applications (Ctrl+C) in a Linux terminal.

In addition to a grasp of the Python programming language, some parts of this book may be difficult to understand without some further knowledge about programming for Linux in general. You will get more out of this book if you already comprehend basic object-oriented concepts. It is also helpful to know how Linux handles processes.

You can still use this book if you do not already know how to implement object orientation or manage processes in Linux, but you may need to supplement this book with one or more online resources. A list of helpful links and tutorials can be found on the book’s web site, which is located at www.gtkbook.com . You can also find more information about the book at www.apress.com .

How This Book Is Organized

Foundations of PyGTK Development is composed of 14 chapters. Each chapter gives you a broad understanding of its topic. For example, Chapter 4 covers container widgets and introduces many of the most important widgets derived from the Gtk.Container class.

Because of this structure, some chapters are somewhat lengthy. Do not feel as though you have to complete a whole chapter in one sitting, because it can be difficult to remember all the information presented. Also, because many examples span multiple pages, consider focusing on just a few examples at a time; try to understand their syntax and intent.

Each chapter provides important information and unique perspectives that help you to become a proficient PyGTK developer.

Chapter 1 teaches you how to install the GTK+ libraries and their dependencies on your Linux system. It also gives an overview of each of the GTK+ libraries, including GObject, GDK, GdkPixbuf, Pango, and ATK.

Chapter 2 introduces the Gtk.Application and Gtk.ApplicationWindow classes. These classes are fundamental classes that wrap the program logic and provide some useful features for your application. While a GTK+ program can be written without utilizing these classes, you will find the creation process much easier and more object-oriented if you utilize these classes.

Chapter 3 steps through two Hello World applications. The first shows you the basic essentials that are required by every GTK+ application. The second expands on the first while also covering signals, callback functions, events, and child widgets. You then learn about widget properties and the Gtk.Button and Gtk.Label widgets.

Chapter 4 begins by introducing the Gtk.Container class. Next, it teaches you about horizontal and vertical boxes, grids, fixed containers, horizontal and vertical panes, notebooks, and event boxes.

Chapter 5 covers basic widgets that provide a way for you to interact with users. These include toggle buttons, specialized buttons, text entries, and spin buttons.

Chapter 6 introduces you to the vast array of built-in dialogs. It also teaches you how to create your own custom dialogs.

Chapter 7 is a general overview of the most useful features of Python. It covers many Python features that are directly useful to the GTK+ programmer but not necessarily covered in depth in many Python introductory texts.

Chapter 8 introduces you to scrolled windows. It also gives in-depth instructions on using the text view widget. Other topics include the clipboard and the Gtk.SourceView library.

Chapter 9 covers two types of widgets that use the Gtk.TreeModel object. It gives an in-depth overview of the tree view widget and shows you how to use combo boxes with tree models or strings.

Chapter 10 provides two methods of menu creation: manual and dynamic. It covers menus, toolbars, pop-up menus, keyboard accelerators, and the status bar widget.

Chapter 11 is a short chapter about how to design user interfaces with the Glade user interface builder. It also shows you how to dynamically load your user interfaces using Gtk.Builder .

Chapter 12 teaches you how to create your own custom GTK+ widgets by deriving them from other widgets.

Chapter 13 covers many of the remaining widgets that do not quite fit into other chapters. This includes several widgets that were introduced in GTK+ 2.10, including recent files and tray icon support.

Chapter 14 gives you a few longer, real-world examples. They take the concepts you have learned throughout the book and show you how they can be used together.

The appendixes act as references to widget properties, signals, styles, stock items, and descriptions of exercise solutions.

Official Web Site

You can find additional resources on the book’s official web site, found at www.gtkbook.com . This web site includes up-to-date documentation, links to useful resources, and articles that supplement what you learn in this book. There is also find a link to the downloadable source code for every example in this book. The Apress web site ( www.apress.com ,) is another great place to find more information about this book.

When you unzip the source code from the web site, you will find a folder that contains the examples in each chapter and an additional folder that holds exercise solutions. You can run all the files within the current folder.

Acknowledgments

I would like to express my gratitude to the many people who have made this book possible. Many thanks go to Daniel Berrange of Red Hat, whose assistance has certainly decreased the number of errors in the book. I would also like to thank Peter Gill and Jonathan Giszczak for their fine technical reviewing skills. You were very tough on every paragraph I wrote and every example I coded, but this book is better today because of the hard work you put into the project.

I would like to extend a special thanks to Andrew Krause for his encouragement and help. Without him, this update to his original book would not have been possible.

In addition, I would like to thank the people at Apress who put so many hours of hard work into the book. I could not imagine writing for any other publisher. It is a great organization that makes the writing process enjoyable.

Finally, I need to acknowledge my wife, who has supported me in every step of the process. Without you, I would not be who I am today and for that I am forever grateful.

—W. David Ashley

Table of Contents

Events 482
Pango 537
Index 561

About the Author and About the Technical Reviewers

About the Author

W. David Ashley
../images/142357_2_En_BookFrontmatter_Figb_HTML.jpg

is a technical writer for SkillSoft, where he specializes in open source, particularly Linux. As a member of the Linux Fedora documentation team, he recently led the Libvert project documentation and wrote the Python programs included with it. He has developed in 20 different programming languages during his 30 years as a software developer and IT consultant. This includes more than 18 years at IBM and 12 years with American Airlines.

 

About the Technical Reviewers

Jonathan Giszczak
../images/142357_2_En_BookFrontmatter_Figc_HTML.jpg

is a professional software developer with extensive experience writing software for the military and financial services industries, as well as the game industry. He graduated from the University of Michigan with a degree in computer engineering. He has been writing C, C++, and Python applications since the 1990s, including applications in Motif and PyGTK.

 
Peter Gill
../images/142357_2_En_BookFrontmatter_Figd_HTML.jpg

loves spending time with his family in Newfoundland, Canada. He is currently a software developer at TownSuite, where he specializes in release deployment and leading a full stack web development team. Peter loves learning programming language and has used Python, Ruby, Rust, Io, Prolog, Java, C, C++, C#, VB, JavaScript, Typescript, Bash, PowerShell and is currently focused on C# with ASP .NET Core and Typescript. He is a huge advocate open source software. He loves to use Git, Jenkins, Docker, and other tools related to automated deployments.

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

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