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
-
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.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.