Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know all style options of a ttk widget?

Tags:

I am without knowledge of tck/tk. I have done carefully search on the internet but haven't found a good solution.

For example, I created a LabelFrame using:

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

Then I need to set the frame style. There is foreground for tk.LabelFrame. However, I didn't find such style option for ttk.LabelFrame on NMT and tck/tk reference. Then I have to guess, like following

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

But this doesn't work, the right thing is:

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

So, my question is, how can I find out all the style options a ttk widget has. Is there some function like:

s.getAllOptions('TLabelframe') 

and then the output is something like:

['background', 'foreground', 'padding', 'border', ...] 
like image 490
Nan Zhou Avatar asked Jul 29 '17 12:07

Nan Zhou


People also ask

What is style in TTK?

Generally, the style name of a ttk widget starts with the letter 'T' followed by the widget name, for example, TLabel and TButton . In Tkinter, every widget has a default widget class. A widget class defines the default style for a widget.

How many types of widgets are available in tkinter?

Ttk comes with 18 widgets, twelve of which already existed in tkinter: Button , Checkbutton , Entry , Frame , Label , LabelFrame , Menubutton , PanedWindow , Radiobutton , Scale , Scrollbar , and Spinbox .

What is the difference between Tk and TTK?

Tkinter widgets are used to add Buttons, Labels, Text, ScrollBar, etc., however, tkinter. ttk supports a variety of widgets as compared to tkinter widgets. Tkinter. ttk doesn't support Place, Pack() and Grid(), thus it is recommended to use tkinter widget with ttk.


2 Answers

I found your question interesting as I had asked myself the same question but have not found time to address it until now. I have written a function called stylename_elements_options(stylename) to do just this. Sharing it here. Hope it can benefit you (although it is 6 months late) and any tkinter users asking 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') 
like image 147
Sun Bear Avatar answered Sep 16 '22 14:09

Sun Bear


The issue is that if you really want to control a style in detail you need to use the layout. So first identify 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 made the trick for me and finally provides me a way to control my ttk widget!!!

Luca

like image 30
LucaG Avatar answered Sep 18 '22 14:09

LucaG