Home > database >  Tkinter cross-platform compatability
Tkinter cross-platform compatability

Time:01-09

I have written a set of customized tkinter widgets, defined as classes, and loaded into the main app as modules. I am working in Windows 10, but have specific concerns in three areas in regard to compatibility with Linux and Mac. These are shown below.

  1. Fonts

I am sticking with tkinter default fonts, and defining the desired font within each individual custom widget. I have found, surprisingly, that I can successfully specify fonts as follows, naming 'TkDefaultFont' just as I might name 'Arial' for example.

font=('TkDefaultFont',11)
font=('TkDefaultFont',10,'bold')
font=('TkDefaultFont',10,'italic')

Would this approach work across Linux and Mac as well as windows?

  1. Importing modules

All of the resources for my main app are stored in a Folder named 'AppAssets' (which is in the same folder as the main app). The custom widgets are stored inside that folder, in another folder named 'TkMods'. In Windows, I am successfully importing these modules as follows, specifying a relative path:

from AppAssets.TkMods import ModButton

Again, would this work across Linux and Mac? If not, is there a line or lines of code that would work instead across all three platforms?

  1. Importing image files

Many of the modules use custom image files (such as a rounded button image, for example). I am importing these as follows, again specifying a relative path.

btnimg = tk.PhotoImage(file="AppAssets/TkMods/Button.png")

Again, would this work cross-platform? If not, is there a single solution that would work across Windows, Mac and Linux?

Any advice appreciated.

CodePudding user response:

I have found, surprisingly, that I can successfully specify fonts as follows, naming 'TkDefaultFont' just as I might name 'Arial' for example... Would this approach work across Linux and Mac as well as windows?

It works, but probably not the way you think. You could use 'NotARealFont' instead of 'TkDefaultFont' and get the same results. The first parameter when defining a font as a tuple is a font family, and TkDefaultFont is not the name of a valid font family. It's the name of an internal font object, which is not the same thing. When you don't give a valid font family, tkinter will fall back to using the font defined by TkDefaultFont.

I am successfully importing these modules as follows... Again, would this work across Linux and Mac?

Yes, importing python modules works the same on platforms. This isn't anything unique to tkinter.

Many of the modules use custom image files ... I am importing these as follows, again specifying a relative path... Again, would this work cross-platform?

It should work the same on all platforms. Note that "work the same" also means it will fail in the same way on all platforms. The path is relative to the current working directory which may or may not be the same as the directory with the script.

  • Related