I am new at tkinter, and somewhat new at python. I have an application where I need to know the parameters (font used: Arial, Calibri, etc..., size, color: foreground I believe in tkinter, effect: normal, bold, italic, etc..) of the default font TkDefaultFont used by my widgets. Today I don't even know what color (foreground in tkinter) to go back to after I change it, since I have no way to "read" the present parameter settings. I have an Entry widget and will validate the input. If the input is invalid, I will change the color (foreground) to red. The code below tells you what I have done, what I know and don't know. Any help will be very appreciated!
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
def JMCheckButton2Command(*args):
if JMCheckButton2Variable.get()==1:
JMEntry.config(foreground = 'red')
else:
JMEntry.config(foreground = 'black')
####################### Tk() #######################
JMWindow = Tk()
s=ttk.Style()
print('\nTheme names are ', s.theme_names())
print('\nLayout of style TButton is:\n', s.layout('TButton'))
print('\nLayout of style TEntry is:\n', s.layout('TEntry'))
print("\nOptions available for element 'Button.label':\n",
s.element_options('Button.label'))
print("\nOptions available for element 'Entry.textarea':\n",
s.element_options('Entry.textarea'))
print("\nFont used in style 'TButton': ", s.lookup('TButton', 'font'))
print("\nFont used in style 'TButton': ", s.lookup('TEntry', 'font'))
####################### ttk.Frame #######################
JMFrame2=ttk.Frame(JMWindow, width='1i', height='2i', borderwidth='5',
relief='raised', padding=(5,5))
JMFrame2.grid()
####################### ttk.Entry #######################
JMEntryVariable = StringVar()
JMEntry = ttk.Entry(JMFrame2, textvariable=JMEntryVariable, width = 25)
JMEntry.insert(0, '100') # insert new text at a given index
####################### ttk.Label #######################
JMLabel2Text2Display = StringVar()
JMLabel2Text2Display.set('Enter number: ')
JMLabel2 = ttk.Label(JMFrame2, textvariable = JMLabel2Text2Display,
justify = 'center')
####################### ttk.Checkbutton #######################
JMCheckButton2Variable = IntVar()
JMCheckButton2=ttk.Checkbutton(JMFrame2, text='Change font color',
command = JMCheckButton2Command, variable = JMCheckButton2Variable)
JMLabel2.grid(column=0, row=0)
JMEntry.grid(column=1, row=0)
JMCheckButton2.grid(column=2, row=0, sticky = 'w', padx=25)
JMWindow.mainloop()
font = tkFont.nametofont('TkDefaultFont')
andfont.actual()
will return the font attributes. But you need to callcget('foreground')
on the widget to get the color. – acw1668JMEntry.config(foreground = '', font=('Segoe UI','9'))
. – Jean-Marc