© 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_1

1. Getting Started

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

Welcome to Foundations of PyGTK Development. In this book, you acquire a thorough knowledge of the GIMP Toolkit (GTK+), which allows you to create comprehensive graphical programs. Before continuing, you should be aware that this book is aimed at Python programmers, so we assume that you already have a good understanding of the Python language, and you can jump right into using GTK+. Time is not spent on bringing you up to speed on Python.

To get the most out of this book, you should follow each chapter sequentially and study all the examples in each chapter. Getting started with GTK+ on Linux is very easy because most distributions are bundled with everything you need to create and run Python/GTK+ programs. We cover Windows and macOS installation procedures later in this chapter.

There are a few tools that should be installed to get you started without running into trouble. First, Python 3.x should be installed. It is required to run GTK+ 3.x Python programs. Second, the GTK+ 3.x runtime libraries should be installed. These libraries come with many dependencies installed, including GObject, Pango, GLib, GDK, GdkPixbuf, and ATK. Be sure to install all the dependent libraries.

You do not need to install the GNU Compiler Collection. You are not compiling any C/C++ programs in the examples provided in this book. You only need Python 3.x and the GTK+ 3.x runtime libraries to be installed to run the example programs.

Differences Between GTK+ 2.x and 3.x

If you are proficient in GTK+ 2.x, you may be surprised by the changes in version 3.x. There are both small and large changes to the GTK+ API and the Python classes that wrap those libraries. While the basics for most widgets are unchanged, there are a lot of small “gotchas” that can cause you grief until you understand why and where the changes have been made.

The reason for most of these changes is due to a change in the GTK+ philosophy. The GTK+ 2.x libraries were designed around consistency between all GTK+ programs, with the use of GTK+ themes as the basis for that consistency. This philosophy completely changed with the GTK+ libraries. While themes are still available, it is now easier to create GTK+ programs that have their own look and feel separate from the current GTK theme. While this gives the developer greater control, it also requires some extra programming steps to achieve the look and feel. It also removes some APIs that make a widget easy to create and control.

The following is a partial list of the differences between GTK+ 2.x and 3.x. Some of these items have simple workarounds, but others require a little more work on the programmer’s part because they are different enough to cause source code porting problems.
  • Many standard stock icons have been removed, mostly the ones used on push buttons and menu items. If you need these icons, you must provide your own set.

  • All the 2.x constants are now grouped in a 3.x Python class as attributes. If you are porting source code, this is a major area that needs to be addressed.

  • Some containers have been eliminated. For instance, the Gtk.Hbox and Gtk.Vbox widgets have been removed and you now must specify the orientation of a Gtk.Box via a parameter when creating a new Gtk.Box instance. Note that the Gtk.Box class is now a real class in GTK+ 3.x, not an abstract class.

  • Default packing for containers has been removed; all packing parameters must be supplied to the API.

  • Some standard dialogs have been removed. You must create your own dialogs to replace them.

  • There are two new major classes that are very useful for the overall control of large and small applications: the Gtk.Application class and the Gtk.ApplicationWindow class. While these classes are not strictly needed for simple applications, you still find them useful for even the simplest of applications. For that reason, we base all the examples in this book on these two classes to wrap our widget examples.

Creating menus is much easier using the Gtk.Application and Gtk.ApplicationWindow classes. This required complex programming in the GTK+ 2.x environment and is reduced to creating an XML file to represent the menu you want to create in the 3.x environment.

Installing GTK+ 3.x

Before you can create programs, you must install Python, GTK+, and all the dependent libraries. This section covers installing GTK+ on Linux and other Unix-like operating systems. It does not cover how to install GTK+ on macOS or Windows. You need to research the correct way to install GTK+ and Python in those OS environments.

Most modern Linux distributions include Python and GTK+ as part of their respective repositories. You simply need to select Python 3 (this is sometimes installed by default) and GTK+ 3.x (use the latest version available, as shown in Figure 1-1) from the package install program in your Linux distribution and then install those packages along with all the dependent packages.

To test your installation, run the following command.
/usr/bin/gtk3-demo
../images/142357_2_En_1_Chapter/142357_2_En_1_Fig1_HTML.jpg
Figure 1-1

GTK+ 3 demo program

If the program exists and the widget documentation window appears, then the GTK+ installation was successful.

Summary

This chapter introduced GTK+ Version 3.x and Python 3 along with some installation prerequisites. It presented some post-installation tests to ensure that GTK+ was successfully installed. And it discussed some differences between GTK+ 2.x and 3.x.

After successfully installing GTK+ 3.x and Python 3, your environment should be ready to build your first Python/GTK+ program.

Chapter 2 further discusses Gtk.Application and the Gtk.ApplicationWindow, the base classes that you should use for all Python 3 GTK+ 3.x programs.

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

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