Graphical Python Apps in Minutes

Organizing your Widgets

Then the fun part starts: adding widgets to the window. This time we append .grid() onto each widget creation, specifying the row and column inside the root window where the widget should appear. This gives us some basic control of the layout inside the window (think of it like a grid) and also gives us the benefit that we don't need to "pack" each widget after creating it – that happens automatically when we specify the position.

So, the first widget we create is a simple static label that says Area:. The next label, area_value, displays the contents of the special TkInter area string variable we declared earlier. Underneath these two labels, we have another static label for Radius:, and then we put an Entry (text entry) widget alongside this, to the right. We set this to be seven characters in width and tell TkInter that the value entered by the user should be stored in the radius string variable we created beforehand.

Finally, underneath all of this – on row three – we add a Calculate button, linking it to the calc_area function we defined early in the program. If you go back and look at that function now, it should be clear how it works. Using a temporary variable called area_result, we calculate the area from the radius. Then, on the second line of the function, we use our TkInter area variable's set method to update it – providing it with the contents of area_result, rounded down to two decimal points (just to make the app look tidier). We need to use this set method to update the TkInter string variable; we can't just change it like a normal variable.

The final line in the program runs the main loop as usual. Try it out – it should look like Figure 3. Enter a radius, click Calculate, and see the result. Then try the options in the File menu that we created. Once you're familiar with how the program works, try experimenting with the layout – moving the position of the Calculate button, rearranging the labels, and so forth. You can also play around with the menu items, adding another submenu and calls to other functions.

Figure 3: Here's a fully working app with a text entry box, labels, buttons, and menu options.

With the widgets we've used in this app, you can create some rather complex applications – but there's even more to explore. You can pop up file load and save dialogs, draw graphics on a canvas, and do much more as well (Figure 4). When you're fully familiar with all the code in our area calculator app, you can explore further features of TkInter on the wiki (Figure 5) [5]. There you'll find links to reference guides and other tutorials. Happy hacking, and if you create an awesome app, let us know all about it!

Figure 4: Syntax highlighting makes it much easier to work with code. Even Nano can highlight Python.
Figure 5: The Python wiki has heaps of resources for learning TkInter in more detail.

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs

News