Tkinter跨平台兼容性

huangapple go评论80阅读模式
英文:

Tkinter cross-platform compatability

问题

  1. 字体

我正在使用 tkinter 的默认字体,并在每个自定义小部件中定义所需的字体。令人惊讶的是,我发现我可以成功地按照以下方式指定字体,就像我可能会为 Arial 一样命名 TkDefaultFont 一样。

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

这种方法是否在 Linux 和 Mac 上与 Windows 一样有效?

  1. 导入模块

我的主应用程序的所有资源都存储在名为 'AppAssets' 的文件夹中(与主应用程序位于同一文件夹中)。自定义小部件存储在该文件夹内,另一个名为 'TkMods' 的文件夹中。在 Windows 中,我成功地按照以下方式导入这些模块,指定相对路径:

from AppAssets.TkMods import ModButton

同样,这在 Linux 和 Mac 上是否有效?如果不行,是否有一行或多行代码可以在这三个平台上都起作用?

  1. 导入图像文件

许多模块使用自定义图像文件(例如圆形按钮图像)。我再次按照以下方式导入这些图像,指定相对路径。

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

同样,这是否跨平台有效?如果不是,是否有一个适用于 Windows、Mac 和 Linux 的单一解决方案?

任何建议都会赐教。

英文:

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.

答案1

得分: 0

以下是翻译好的内容:

我出奇制胜地发现,我可以成功地指定字体,如下所示,只需像为“Arial”一样命名“TkDefaultFont”...
这种方法会在Linux、Mac和Windows上都奏效吗?

它是有效的,但可能不是你想的那种方式。你可以使用“NotARealFont”代替“TkDefaultFont”并获得相同的结果。在将字体定义为元组时的第一个参数是字体系列,而“TkDefaultFont”不是有效字体系列的名称。它是内部字体对象的名称,这不是同一回事。当你没有提供有效的字体系列时,tkinter会回退到使用由“TkDefaultFont”定义的字体。

我正在成功导入这些模块,如下所示...同样,这在Linux和Mac上能工作吗?

是的,导入Python模块在不同平台上都是相同的。这并不是tkinter特有的。

许多模块使用自定义图像文件...我正在如下导入它们,再次指定相对路径...这在跨平台上会奏效吗?

这应该在所有平台上都能正常工作。请注意,“工作相同”也意味着它在所有平台上会以相同的方式失败。路径是相对于当前工作目录的,这可能与脚本所在的目录相同,也可能不同。

英文:

> 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.

huangapple
  • 本文由 发表于 2023年1月9日 10:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定