In this chapter we will give a simple introduction to the Python versions of the Tkinter and
Qt graphics systems. Such a task could fill a whole book, so for this chapter we will keep
things very simple and aim to give an impression of the basic principles and what kinds of
things are possible: to help a programmer get started, rather than give an in-depth
description of every possible widget. The examples construct a simple graphical interface
for some of the biological sequence analysis that was discussed in
Chapter 11
. Indeed, we
will import functions defined there.
3
We will only show a few of the more basic widget
types, but the interface will be functional. The same basic interface will be constructed
using both Tkinter and Qt, so that you can compare and contrast the differences. There
might be a more elegant way of handling things in either example, but we have tried to
keep the equivalence reasonably close.
Compared to most of the other chapters, the reader will notice that the examples are
constructed by using classes, which generate a customised version of a graphical Python
object. Whereas in most of the examples in this book we can illustrate the principles by
sticking predominantly to simpler Python functions (and leave it to the programmer to
decide whether to use classes), here for the GUI code we actively encourage using classes
from the outset. The reason for this is the high degree of interconnectivity and data sharing
that goes on with graphical items and the underlying data they represent or manipulate.
Making a class for a graphical interface, which usually at least requires a subclass of a top-
level window, allows all of the component widgets to be automatically grouped. Also, it is
less sensitive to the order in which the code is written and minimises the arguments that
have to be passed; internal functions can access class attributes (variables that belong to
the GUI object). A GUI can be written with functions alone, but it is harder work.
The interfaces that form the examples are pictured in
Figure 26.2
. Each example
consists of a single top-level window and inside this we place the sub-widgets: small text
labels to provide headings, large text boxes to display DNA sequences and output, buttons
to allow the user to trigger actions and a small text entry box for a user to type into. It is
deliberate that we have accepted the default styling for the widgets and not tried to
customise the look; that comes later if you can find the time. The examples only
demonstrate a few of the available widget types, but a more complete listing is available in
the on-line documentation for Tkinter and Qt.
Do'stlaringiz bilan baham: