Python 中 tkinter 和 tkinter.ttk 的小部件有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19561727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:04:06  来源:igfitidea点击:

What is the difference between the widgets of tkinter and tkinter.ttk in Python?

pythontkinterttk

提问by multigoodverse

The main tkintermodule and its submodule ttkin Python 3 appear to contain identical widgets (i.e. Buttons, CheckButtons, etc.).

tkinter模块和子模块其ttk在Python 3看起来包含相同窗口小部件(即ButtonsCheckButtons等)。

So, when creating a button, one has the freedom to either use a tkinter.Buttonwidget or a tkinter.ttk.Button.

因此,在创建按钮时,可以自由使用tkinter.Button小部件或tkinter.ttk.Button.

Do you know what is the difference between them? Why would you choose one or the other?

你知道它们之间有什么区别吗?你为什么会选择其中之一?

采纳答案by Bryan Oakley

The widgets in tkinterare highly and easily configurable. You have almost complete control over how they look - border widths, fonts, images, colors, etc.

中的小部件tkinter高度且易于配置。您几乎可以完全控制它们的外观——边框宽度、字体、图像、颜色等。

ttkwidgets use styles to define how they look, so it takes a bit more work if you want a non-standard button. ttkwidgets are also a little under-documented. Understanding the underlying theme and layout engines (layout within the widgets themselves, not pack, gridand place) is a challenge.

ttk小部件使用样式来定义它们的外观,因此如果您想要一个非标准按钮,则需要做更多的工作。ttk小部件也有一些记录不足。理解底层的主题和布局引擎(小部件本身的布局,而不是pack,gridplace)是一个挑战。

Generally speaking, the themed widgets will give you an application that looks more "native", but at the expense of a loss of configurability.

一般来说,主题小部件会给你一个看起来更“原生”的应用程序,但代价是可配置性的损失。

My advice is to use ttkwidgets if you want your GUI to look a little more modern, and the tkinter widgets if you need a bit more configurability. You can use them both in the same applications.

我的建议是,ttk如果您希望 GUI 看起来更现代,请使用小部件,如果您需要更多的可配置性,请使用 tkinter 小部件。您可以在同一个应用程序中同时使用它们。

回答by Larz60p

You may want to take a look at Converting existing applications to use the Tile widgets

您可能需要查看转换现有应用程序以使用 Tile 小部件

Also see:

另见:

Tk Widget Styling Support

Tk 小部件样式支持

As stated in this document:

如本文件所述:

Recently, other Open Source toolkits such as Qt (used by the KDE project) and GTK (used by the GIMP graphics editing software and the Gnome project) emerged as powerful and free alternatives to Motif for X-Window GUI development. The rapidly growing success of Open Source systems such as GNU/Linux helped both toolkits attract a vast community of developers, and the firm (and sometimes friendly) competition between both communities led to an explosion of new features. Thirst for freedom and customizability created the need for themeability.

The current implementation of Tk only provides native look&feel on supported platforms (Windows, X-Window, MacOS). This lack partly explains Tk's loss of mind-share, especially amongst Linux developers, where theme support is considered a "cool" or must-have feature.

While yesterday's goal of many GUIs was cross-platform visual uniformity (Qt and GTK borrowed much of their visual appearance from Windows, which borrowed earlier from NeXTStep), it is now quite common to find huge visual differences on today's desktops, even on similar systems. Screenshot contests are quite common nowadays.

...

Many Tk users may see themes support as cosmetic or of lower importance than much needed features such as megawidgets or objectification. Nevertheless, this is a critical feature to be implemented for the long-term viability of Tk. Many courses are now promoting Qt, GTK or (aarggg!) Swing in place of Motif, leaving no room for Tk. Whatever its qualities (cross-platform, performance, ease of use, internationalization and Unicode support), the lack of themeability will always be seen as one of the main reasons for not using Tk. Applications using Tk instead of GTK will look as "foreign" on pixmap-themed Linux desktop, or even on newer MacOS and Windows versions, as pre-8.0 applications were on non-X desktops.

最近,其他开源工具包,如 Qt(由 KDE 项目使用)和 GTK(由 GIMP 图形编辑软件和 Gnome 项目使用)作为 Motif 的强大且免费的替代品出现,用于 X-Window GUI 开发。开源系统(如 GNU/Linux)的迅速增长的成功帮助这两个工具包吸引了庞大的开发人员社区,并且两个社区之间的激烈(有时是友好的)竞争导致了新功能的爆炸式增长。对自由和可定制性的渴望创造了对主题性的需求。

Tk 的当前实现仅在支持的平台(Windows、X-Window、MacOS)上提供本机外观。这种缺乏部分解释了 Tk 失去思想共享,特别是在 Linux 开发人员中,主题支持被认为是“很酷”或必须具备的功能。

虽然昨天许多 GUI 的目标是跨平台视觉一致性(Qt 和 GTK 的大部分视觉外观都从 Windows 中借用,后者更早地从 NeXTStep 中借用),但现在在今天的桌面上发现巨大的视觉差异是很常见的,即使在类似的系统上也是如此. 截图比赛现在很常见。

...

许多 Tk 用户可能认为主题支持是装饰性的,或者比大部件或对象化等急需的功能更重要。尽管如此,对于 Tk 的长期生存能力来说,这是一个需要实施的关键特征。许多课程现在都在推广 Qt、GTK 或(aarggg!)Swing 来代替 Motif,为 Tk 留出空间。无论其品质如何(跨平台、性能、易用性、国际化和 Unicode 支持),缺乏主题性将始终被视为不使用 Tk 的主要原因之一。使用 Tk 而不是 GTK 的应用程序在以像素图为主题的 Linux 桌面上看起来像是“外来的”,甚至在较新的 MacOS 和 Windows 版本上,因为 8.0 之前的应用程序在非 X 桌面上。

There are some widgets (6 total) that are part of ttk, and not tkinter. there are, as stated above, some configuration items missing, like fg and bg, but this can be done with style, (introduced in tk 8.5).

有一些小部件(总共 6 个)是 ttk 的一部分,而不是 tkinter。如上所述,缺少一些配置项,例如 fg 和 bg,但这可以通过样式完成(在 tk 8.5 中引入)。

Using both together, with tkinter.ttk overloading tkinter gives you the best of both worlds.

将两者结合使用,通过 tkinter.ttk 重载 tkinter 为您提供两全其美的效果。

Some of the additional widgets in ttk are very useful (there are 6 that are not found in tkinter), like Notebook (tabbed windows) which I use often.

ttk 中的一些附加小部件非常有用(tkinter 中没有 6 个),例如我经常使用的 Notebook(选项卡式窗口)。

Larz60p

拉兹60p

回答by Jaidee

My opinion for beginners who are starting to learn Tkinter, is to use Tkinterwidgets, because they're really easy to learn. But on the other hand Tkinter.ttkis a module designed to make Tkinterwidgets look really perfectly, but is really hard to learn and there are no easy options there. Like there are no -fg, -bg. Perhaps, there are no new styles available in Tkinter. Style are only designed for ttk, and can be found in ttk.

对于刚开始学习的初学者,我的意见Tkinter是使用Tkinter小部件,因为它们真的很容易学习。但另一方面Tkinter.ttk是一个旨在使Tkinter小部件看起来非常完美的模块,但真的很难学习,而且没有简单的选择。就像没有-fg-bg。也许,Tkinter. 样式仅适用于ttk,并且可以在ttk.

And Tkinterwidgets really don't look like other native platform widgets.

而且Tkinter小部件确实看起来不像其他原生平台小部件。

But ttkis nicer and smoother looking, and look like other native platforms.

但是ttk看起来更好更流畅,并且看起来像其他原生平台。

So if you are making apps for your own private use, then use Tkinterand also use some ttkif needed, because ttksupports much cooler widgets that can change the look of your app.

因此,如果您正在为自己的私人用途制作应用程序,那么请使用Tkinterttk在需要时使用一些,因为ttk支持更酷的小部件,可以改变您的应用程序的外观。

And if you are making apps for public use, then go for both because Tkinteris needed for creating the window and some more important stuff, and for widgets go for ttk.

如果你正在制作供公众使用的应用程序,那么两者Tkinter都需要,因为创建窗口和一些更重要的东西需要,而小部件则需要ttk.

But honestly, I say use both because there are no conflicts between the two; just use them both to your advantage.

但老实说,我说两者都用是因为两者之间没有冲突;只需将它们都用于您的优势。

Honestly using ttkis a challenge! Because it has no Grid,Pack, Placeand many other options that are normally available in Tkinterwidgets. But wait!! Tkinterhas those options! So use both! Try to make a nice app!

诚实使用ttk是一个挑战!因为它没有GridPackPlace和在正常使用许多其他的选择Tkinter部件。可是等等!!Tkinter有这些选择!所以两者都用!尝试制作一个不错的应用程序!

That is the real difference between the two: Tkinterwidgets are more configurable, and ttkis more modern and it is configurable with styles which are really handy shortcuts. And Tkinteris like the core of the window and ttkis styling. Think of it like this:

这就是两者之间的真正区别:Tkinter小部件更可配置,ttk更现代,并且可以使用非常方便的快捷方式进行配置。而且Tkinter就像是窗口的核心,ttk是造型。可以这样想:

Tkinter --- HTML, ttk --- CSS, Python --- JavaScript.

Tkinter --- HTML,ttk --- CSS,Python --- JavaScript。