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.
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!
Infos
- PyQt: https://riverbankcomputing.com/software/pyqt/intro
- PyGObject: https://wiki.gnome.org/action/show/Projects/PyGObject
- Kivy: https://kivy.org
- List of toolkits: https://wiki.python.org/moin/GuiProgramming
- Python wiki: https://wiki.python.org/moin/TkInter
« Previous 1 2
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.