且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Tkinter 在不同的计算机上看起来不同

更新时间:2023-12-06 13:47:46

是的,tkinter 应用程序在不同平台上看起来会有所不同.这是设计使然.如果可能,Tkinter 将使用本机小部件——操作系统提供的小部件.这意味着 Tkinter 无法最终控制 GUI 的各个方面.

Yes, tkinter applications will look different on different platforms. This is by design. When possible, Tkinter will use native widgets -- widgets provided by the OS. This means that Tkinter does not have ultimate control over every aspect of the GUI.

在大多数情况下,这是理想的行为.您希望 Windows 上的按钮看起来像 Windows 按钮,并且希望 OSX 上的按钮看起来像 OSX 按钮.这也意味着您可能会在 Windows 7 和 Windows 8 上获得不同的按钮——tkinter 只是使用操作系统提供的任何东西,而微软倾向于在每次重大修订时调整其小部件.

Under most circumstances this is the desired behavior. You want a button on Windows to look like a Windows button, and you want a button on OSX to look like an OSX button. This also means that you may get different buttons on Windows 7 vs Windows 8 -- tkinter is just using whatever the OS gives it, and Microsoft tends to tweak its widgets with every major revision.

您能否在所有平台上获得完全相同的外观?那要看.只需少量工作,您就可以在 Windows 7 和 8 上获得几乎相同的结果.在 Windows 和 Macintosh 之间获得相同的外观将是非常困难的.

Can you get the exact same look on all platforms? That depends. You can probably get nearly identical results on Windows 7 and 8 with a small amount of work. It will be very difficult to get the same look between Windows and Macintosh.

不幸的是,这些差异不仅会导致小部件外观不同,还会导致布局差异.例如,不同的平台可能有不同的字体、不同的默认边框宽度以及默认的边距和内边距.所有这些都有助于窗口的最终布局.

Unfortunately, these differences not only result in different looking widgets, but it can cause layout differences as well. For example, different platforms may have different fonts, different default border widths, and default margins and padding. All of this contributes to the final layout of a window.

在您的特定情况下,最明显的区别是 Windows 7 使用的字体比 Windows 8 略小.仅此一项就会对布局产生很大影响.看起来 McListBox 小部件在 Windows 7 上的边框比在 Windows 8 上更大.我对该小部件一无所知,因此它可能具有基于平台的不同默认值.

In your specific case, the most obvious difference is that Windows 7 is using a slightly smaller font than Windows 8. That alone can have a big effect on layout. It also appears that the McListBox widget has a larger border around it on windows 7 than on windows 8. I don't know anything about that widget, so it could have different defaults based on the platform.

这些问题中的大多数都可以通过对边框、填充、字体等使用显式值来解决.有时需要一些实验.

Most of these problems can be resolved by using explicit values for borders, padding, fonts, etc. Sometimes it takes a little experimentation.