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

StackOverflow https://stackoverflow.com/questions/19561727

  •  01-07-2022
  •  | 
  •  

Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

My opinion for beginners who are starting to learn Tkinter, is to use Tkinter widgets, because they're really easy to learn. But on the other hand Tkinter.ttk is a module designed to make Tkinter widgets 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.

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

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

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

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

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

Honestly using ttk is a challenge! Because it has no Grid,Pack, Place and many other options that are normally available in Tkinter widgets. But wait!! Tkinter has those options! So use both! Try to make a nice app!

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

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

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

Also see:

Tk Widget Styling Support

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

Ttk can be more customizable, but as others have said, it takes some learning on how to build out custom styles, etc... However, the look and feel is significantly better IMO than the standard tkinter widgets.

If you want to use ttk but also want more control over colors, you can use a library like ttkbootstrap that has dozens of themes and built in color styles for each widget. There are even some non-standard widgets for meters and calendars, etc...

It is just a matter of the design of a GUI Window. So Tkinter is a really easy-to-use efficient and useful GUI toolkit. But it looks a bit outdated. Tkinter Ttk widgets are similar to that of Tkinter. It is basically a sub-module of Tkinter. So, in Tkinter Ttk, almost many widgets are the same but to use them is different. Tkinter Ttk widgets are more customizable and a lot of windows programs are done and designed using Tkinter

from tkinter import ttk
import tkinter

root = tkinter.Tk()

style = ttk.Style()
style.theme_use("default")

ttk.Combobox().pack()
tkinter.Button(root, text="press me", height=10).pack()
ttk.Button(root, text="not me", padding=50).pack(pady=10, padx=10)

root.mainloop()

press to see the diffrence Ttk give you more control over the design(theme)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top