How many / What types of ttk types are available?

I recently made some graphical interfaces and found that the buttons looked pretty soft, so I read a few tutorials on improving the look with ttk and got the following:

from tkinter import ttk from tkinter.ttk import * root = tkinter.Tk() style = ttk.Style() style.configure("BW.TLabel") btn = ttk.Button(text="Test") btn.pack() root.mainloop() 

Yes, it looks better, but I was wondering if there are different ttk styles, and if so, how many are there and where to check them.

+6
source share
3 answers

There are not many built-in styles for ttk, I believe that this is only one style for each type of widget. that ttk allows you to define your own styles derived from built-in functions, customize the bits you want differently, and then easily apply this style to all widgets.

This is a good link to view. but say, for example, that you wanted to change all the ttk buttons to use the red font, which you would do as follows:

 import tkinter as tk from tkinter import ttk from tkinter.ttk import * root = tk.Tk() style = ttk.Style() style.configure("TButton", foreground="red") btn = ttk.Button(text="Test") btn.pack() root.mainloop() 

then each ttk button will have a red foreground.

to create a new style, you will do this:

 import tkinter as tk from tkinter import ttk from tkinter.ttk import * root = tk.Tk() style = ttk.Style() style.configure("Mine.TButton", foreground="red") btn = ttk.Button(text="Test") btn.pack() btn2 = ttk.Button(text="Test", style="Mine.TButton") btn2.pack() root.mainloop() 

so the first button uses the default button style, but I get a new button style called Mine.TButton (.TButton means that it copies the existing TButton style and then changes what you specify in the configure call)

therefore, any button created with this style will look the same.

+2
source

Firstly, some terminology. Ttk is a layer for Tk. It consists of a set of themes that consist of styles applied to widgets. Styles consist of elements provided by one or more element engines.

The purpose of the anti-aliasing layer was to make Tk better in the natural way users desktop users, and not to let the programmer create all kinds of terrible user widgets. This has already been achieved with Tk.

So, on Windows, the default theme is one that can use the visual style API. This is what draws the edges and background of the button, etc. On Windows XP and above. Since Tk supported older versions of Windows when it was created, we also have a theme that supports GDI (winnative) drawing elements. For Tk running on MacOS, there is an element mechanism that uses a native theme, and on X11 we have to use Tk drawing to produce something that looks normal. Attempts to connect Ttk to the GTK + and Qt APIs failed.

Thus, the topics available to you depend on the platform on which you are located. Different themes provide different configurations. Those that use elements provided by an external API (vsapi, winnative, or aqua) do not actually allow you to configure much. However, themes can also import elements from the default element mechanism (Tk draw) or use images as visual elements. The mechanism of the image element allows you to completely change the appearance of the entire collection of styles (and, therefore, the appearance of all widgets), if you really want it. However its pretty slow.

In general, the point was to reduce the work on developers so that applications looked like native ones, and not to ensure maximum customization.

To find available topics:

 >>> ttk.Style().theme_names() ('clam', 'alt', 'default', 'classic') 

It was on Linux. Windows 7 will also have winnative, xpnative, and vista. The initial theme is selected depending on the platform at runtime if the application does not select an alternative theme.

The styles defined in the theme are associated with widgets. There is no method to list all styles. You could iterate over all ttk widgets and view the results of the winfo_class() method, as this shows the theme style used by these widgets.

+2
source
 from ttkthemes import themed_tk as tk #For set Themes from tkinter import ttk #ttk = themed tkinter #Creating themed tkinter window and saving it in root variable root = tk.ThemedTk() # Get a list of all themes that can be set root.get_themes() #Setting theme named radiance root.set_theme("radiance") 
0
source

Source: https://habr.com/ru/post/1234482/


All Articles