The main tkinter
module and its submodule ttk
in Python 3 appear to contain identical widgets (i.e. Buttons
, CheckButtons
, etc.).
So, when creating a button, one has the freedom to either use a tkinter.Button
widget or a tkinter.ttk.Button
.
Do you know what is the difference between them? Why would you choose one or the other?
The widgets in tkinter
are highly and easily configurable. You have almost complete control over how they look - border widths, fonts, images, colors, etc.
ttk
widgets use styles to define how they look, so it takes a bit more work if you want a non-standard button. ttk
widgets are also a little under-documented. Understanding the underlying theme and layout engines (layout within the widgets themselves, not pack
, grid
and place
) is a challenge.
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 ttk
widgets 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.
You may want to take a look at Converting existing applications to use the Tile widgets
Also see:
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.
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).
Using both together, with tkinter.ttk overloading tkinter gives you the best of both worlds.
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.
Larz60p
My opinion for beginners are learning Tkinter
because it's really really easy to learn. Bu on the hand Tkinter.ttk
is a module designed for making Tkinter
widget really perfect, but is really hard and there are no easy options there, just go for the tougher ones. Like there are no -fg
, -bg
. Perhaps, there are a new style unavailable in Tkinter
. It is only designed for ttk
, and can be found in ttk
.
And Tkinter
widgets really doesn't look like other native platform widgets.
But ttk
is more smooth looking, nice and like other nattive platforms.
So if your making apps private for your own use then use Tkinter
and also use some ttk
if needed, because ttk
supports more more cooler widgets that can change the look of your app.
And if your making apps for public, then go for both because Tkinter
is needed because of creating of window and some more important stuff, and for widgets go for ttk
.
But honestly, I say use both because there is no fighting between those two, just use them for your advantage.
ttk
is honestly a challenge! Because it has no Grid
,Pack
, Place
and more and more and even more important stuff. But wait!! Tkinter
has it! So use both! Try to make a nice app!
The real difference between them are that, Tkinter
widgets are more configurable as choice, and ttk
is more modern and it is configurable with styles which are a really handy shortcuts. And Tkinter
is like the core of the window and ttk
is styling. Think Like:
Tkinter --- HTML, ttk --- CSS, Python --- JavaScript.
Hope you had some solution by this answer!
来源:https://stackoverflow.com/questions/19561727/what-is-the-difference-between-the-widgets-of-tkinter-and-tkinter-ttk-in-python