Search code examples
pythontkinterttk

How to determine the font being used by a ttk.Button in tkinter?


I have a ttk button whose font I want to know. Using: my_button.cget('font') gives me _tkinter.TclError: unknown option "-font". Alright, that means the widget itself is not storing the font. So I try to query the style instead with my_style.cget('font'). Then the result is AttributeError: 'Style' object has no attribute 'cget'.

Here is a code snipet. This time I tried to directly change the font. I know it's calibri but can't query it:

my_style = ttk.Style()
my_style.configure('W.TButton', background='black',
                   font=('calibri', 10, 'bold', 'underline'), foreground='white')
print(my_style.cget('font'))

Solution

  • Use my_style.lookup("W.TButton", "font") instead.