How to find out all ttk widget style options

This problem almost makes me spoil. I am new and without tck / tk knowledge. I did a thorough search on the Internet but did not find a good solution.

For example, I created a label frame using

import tkinter as tk from tkinter import ttk newBT = ttk.LabelFrame(width=100, height=100) 

Then I need to set the frame style. In the foreground is tk.LabelFrame . However, I did not find such a style option for ttk.LabelFrame on NMT and tck / tk . Then I have to guess how after

 s = ttk.Style() s.configure('TLabelframe', foreground='red') 

But it doesn’t work, the right thing

 s.configure('TLabelframe.Label', foreground='red') 

So my question is: how can I find out all the style options that the ttk widget has. Is there any kind of function like

 s.getAllOptions('TLabelframe') 

and then the result looks like

 ['background', 'foreground', 'padding', 'border', ...] 

Thanks!

+7
source share
3 answers

I found your question interesting, because I asked myself the same question, but have not found the time to solve it so far. I wrote a function called stylename_elements_options(stylename) to do this. Share it here. Hope this can benefit you (although it is 6 months), and any tkinter users ask the same question.

Script:

 import tkinter as tk import tkinter.ttk as ttk def stylename_elements_options(stylename): '''Function to expose the options of every element associated to a widget stylename.''' try: # Get widget elements style = ttk.Style() layout = str(style.layout(stylename)) print('Stylename = {}'.format(stylename)) print('Layout = {}'.format(layout)) elements=[] for n, x in enumerate(layout): if x=='(': element="" for y in layout[n+2:]: if y != ',': element=element+str(y) else: elements.append(element[:-1]) break print('\nElement(s) = {}\n'.format(elements)) # Get options of widget elements for element in elements: print('{0:30} options: {1}'.format( element, style.element_options(element))) except tk.TclError: print('_tkinter.TclError: "{0}" in function' 'widget_elements_options({0}) is not a regonised stylename.' .format(stylename)) stylename_elements_options('my.Vertical.TScrollbar') 
+3
source

The problem is that if you really want to control the style in detail, you need to use a layout. Therefore, first define the widget class using:

 >>b=ttk.Button(None) >>b.winfo_class() 'TButton 

Then use the command

 >>> s.layout('TButton') [("Button.border", {"children": [("Button.focus", {"children": [("Button.spacing", {"children": [("Button.label", {"sticky": "nswe"})], "sticky": "nswe"})], "sticky": "nswe"})], "sticky": "nswe", "border": "1"})] 

Finally, change what you want:

 s.layout("MYButton.TButton",[("Button.border", {"children": [("Button.focus", {"children": [("Button.spacing", {"children": [("Button.label", {"sticky": "nswe"})], "sticky": "nswe"})], "sticky": "nswe"})], "sticky": "we", "border": "1"})] 

This did the trick for me and finally provided me with a way to control my ttk widget !!!

Luke

+2
source

Based on the SunBear script:

 import tkinter as tk import tkinter.ttk as ttk def iter_layout(layout, tab_amnt=0, elements=[]): """Recursively prints the layout children.""" el_tabs = ' '*tab_amnt val_tabs = ' '*(tab_amnt + 1) for element, child in layout: elements.append(element) print(el_tabs+ '\'{}\': {}'.format(element, '{')) for key, value in child.items(): if type(value) == str: print(val_tabs + '\'{}\' : \'{}\','.format(key, value)) else: print(val_tabs + '\'{}\' : [('.format(key)) iter_layout(value, tab_amnt=tab_amnt+3) print(val_tabs + ')]') print(el_tabs + '{}{}'.format('} // ', element)) return elements def stylename_elements_options(stylename, widget): """Function to expose the options of every element associated to a widget stylename.""" try: # Get widget elements style = ttk.Style() layout = style.layout(stylename) config = widget.configure() print('{:*^50}\n'.format(f'Style = {stylename}')) print('{:*^50}'.format('Config')) for key, value in config.items(): print('{:<15}{:^10}{}'.format(key, '=>', value)) print('\n{:*^50}'.format('Layout')) elements = iter_layout(layout) # Get options of widget elements print('\n{:*^50}'.format('element options')) for element in elements: print('{0:30} options: {1}'.format( element, style.element_options(element))) except tk.TclError: print('_tkinter.TclError: "{0}" in function' 'widget_elements_options({0}) is not a regonised stylename.' .format(stylename)) widget = ttk.Button(None) class_ = widget.winfo_class() stylename_elements_options(class_, widget) 

Prints configuration options as well as a layout tree.

0
source

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


All Articles