22.07.2013 Views

A Comprehensive Introduction to Python Programming and ... - MSDL

A Comprehensive Introduction to Python Programming and ... - MSDL

A Comprehensive Introduction to Python Programming and ... - MSDL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.4 Nothing is perfect . . . 19 / 75<br />

The first 2 lines allow <strong>to</strong> create a complete window. Compared <strong>to</strong> MFC programming,<br />

it makes no doubt that Tkinter is simple <strong>to</strong> use. The third line sets the<br />

caption of the window, <strong>and</strong> the fourth one makes it enter its event loop.<br />

3. Portability <strong>Python</strong> scripts that use Tkinter do not require modifications <strong>to</strong> be<br />

ported from one platform <strong>to</strong> the other. Tkinter is available for any platform<br />

that <strong>Python</strong> is implemented for, namely Microsoft Windows, X Windows, <strong>and</strong><br />

Macin<strong>to</strong>sh. This gives it a great advantage over most competing libraries, which<br />

are often restricted <strong>to</strong> one or two platforms. Moreover, Tkinter will provide the<br />

native look-<strong>and</strong>-feel of the specific platform it runs on.<br />

4. Availability Tkinter is now included in any <strong>Python</strong> distribution. Therefore, no<br />

supplementary modules are required in order <strong>to</strong> run scripts using Tkinter.<br />

2.4 Nothing is perfect . . .<br />

Tkinter has many advantages that make it the primary choice for UI design. However,<br />

it has a drawback which originates directly from its imlementation: it has a significant<br />

overhead due <strong>to</strong> its layered implementation. While this could constitute a problem with<br />

older, slower machines, most modern computers are fast enough so as <strong>to</strong> cope with the<br />

extra processing in a reasonable amount of time. When speed is critical, however,<br />

proper care must be taken so as <strong>to</strong> write code that is as efficient as possible.<br />

Even if the overhead tends <strong>to</strong> become less relevant with time, there is still a disadvantage<br />

<strong>to</strong> this layered implementation that is not going <strong>to</strong> fade away: the source<br />

code for the Tkinter library is only a shell that provides Tk’s functionality <strong>to</strong> <strong>Python</strong><br />

programs. Therefore, in order <strong>to</strong> underst<strong>and</strong> what the Tkinter source code does, programmers<br />

need <strong>to</strong> underst<strong>and</strong> the source code of the Tk library, which is in turn written<br />

in C. While certainly not impossible, this requires more work from programmers <strong>and</strong><br />

can be quite time consuming.<br />

2.5 Show me what you can do!<br />

In order <strong>to</strong> fully underst<strong>and</strong> the advantages that Tkinter has <strong>to</strong> offer, it is necessary <strong>to</strong><br />

see some examples demonstrating just how simple programming using this <strong>to</strong>olkit can<br />

be. Let’s start with a more detailed explanation of the example presented above. For<br />

simplicity, the listing of the complete source of the example is reproduced again below.<br />

from Tkinter import *<br />

root = Tk( )<br />

root.title(’A simple application’)<br />

root.mainloop( )<br />

Listing 20: A very simple application

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!