Reader small image

You're reading from  Python GUI Programming with Tkinter, 2nd edition - Second Edition

Product typeBook
Published inOct 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801815925
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Alan D. Moore
Alan D. Moore
author image
Alan D. Moore

Alan D. Moore is a data analyst and software developer who has been solving problems with Python since 2006. He's developed both open source and private code using frameworks like Django, Flask, Qt, and of course Tkinter, and is known to contribute to various open-source Python and JavaScript projects. Alan maintains a YouTube channel, “Alan D Moore Codes”, where he posts Python, PyQt, and Tkinter tutorials. Alan lives in Franklin, Tennessee, where he works for the County Government, and with his wife Cara raises a crew of children who are just as geeky as their dad.
Read more about Alan D. Moore

Right arrow

Maintaining Cross-Platform Compatibility

Word has spread throughout ABQ AgriLabs about your application, and it is being requested as a way to visualize and work on experimental data files. As a result, it now needs to run on Windows, macOS, and Linux systems equally well. Fortunately for you, Python and Tkinter are supported on these three operating systems, and you'll be pleasantly surprised to find that your application already runs unaltered on all three. However, there are some small issues that you need to address and remain aware of in order for your application to be a good citizen on each platform.

In this chapter, we'll learn more about cross-platform compatibility as we cover the following topics:

  • In Writing cross-platform Python, you'll learn how to keep basic Python code functional across multiple platforms.
  • In Writing cross-platform Tkinter, you'll learn about cross-platform issues that affect Tkinter code specifically.
  • ...

Writing cross-platform Python

At the time of writing, Python is supported on nearly a dozen operating system platforms, covering everything from common desktop systems like Windows to high-end commercial Unixes like AIX and obscure OS projects such as Haiku OS.

Across all these platforms, most Python code works without any significant alteration, as Python has been designed to translate high-level functionality into appropriate low-level operations on each system. Even so, there are situations where OS differences cannot be (or simply have not been) abstracted away, and careful handling is required to avoid platform-specific failures.

In this section, we'll look at some of the larger issues that impact cross-platform Python.

Filenames and file paths across platforms

Filesystems are probably the biggest source of pitfalls for cross-platform development. Although most platforms share the concept of files and directories arranged in a hierarchy, there are some crucial...

Writing cross-platform Tkinter

As you've seen so far, Tkinter mostly works identically across platforms, and even has the capability to do the right thing on each platform with minimal effort. However, there are some minor issues to be aware of as you support a Tkinter application across multiple operating systems. In this section, we'll explore the more significant differences.

Tkinter version differences across platforms

As of 2021, the official Python 3 distributions for major platforms ship at least Tcl/Tk 8.6; this is the latest major release of Tcl/Tk and includes all the functionality discussed in this book. However, not every platform includes the latest minor version, which may impact bug fixes and minor features. At the time of writing, the latest version of Tcl/Tk is 8.6.11.

Historically, some platforms (notably macOS) have lagged behind in shipping the latest version of Tcl/Tk. While platform support at the time of writing is fairly consistent...

Improving our application's cross-platform compatibility

Our application does pretty well across platforms, but there are some things we can do to improve it:

  • First, our application stores its preferences in the user's home folder, which is not ideal on any platform. Most desktop platforms define specific locations where configuration files should be placed, so we will fix our application to use those for the abq_settings.json file.
  • Second, we're creating our CSV files without specifying any encoding; if a user inserted a Unicode character (say, in the Notes field), file saving would raise an exception and fail on non-Unicode platforms.
  • Finally, the current menu structure does not really come close to following any of the human interface guidelines we've discussed. We'll implement separate menus for each platform to ensure users have a UI that is consistent with their platform.

Let's get started!

Storing preferences...

Summary

In this chapter, you learned how to write Python software that works well across multiple platforms. You learned how to avoid common platform pitfalls in Python code such as filesystem differences and library support, and how to write software that intelligently adapts to the needs of different operating systems. You also learned about published guidelines that help developers write software that meets platform users' expectations, and you used these guidelines to create platform-specific menus for ABQ Data Entry.

In the next chapter, we're going to learn about automated testing. You'll learn to write tests that ensure your code works correctly, both for regular Python code and specifically for Tkinter code, and to take advantage of the testing framework included in the Python standard library.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming with Tkinter, 2nd edition - Second Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801815925
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alan D. Moore

Alan D. Moore is a data analyst and software developer who has been solving problems with Python since 2006. He's developed both open source and private code using frameworks like Django, Flask, Qt, and of course Tkinter, and is known to contribute to various open-source Python and JavaScript projects. Alan maintains a YouTube channel, “Alan D Moore Codes”, where he posts Python, PyQt, and Tkinter tutorials. Alan lives in Franklin, Tennessee, where he works for the County Government, and with his wife Cara raises a crew of children who are just as geeky as their dad.
Read more about Alan D. Moore