Python Tkinter custom themes Python Tkinter custom themes tkinter tkinter

Python Tkinter custom themes


The standard style applied to ttk.Entry simply doesn't take a fieldbackground option, which would be what changes the colour of the text entry field. The solution is this to create a new element that does respond to the option.

from tkinter import *from tkinter import ttkroot_window = Tk()estyle = ttk.Style()estyle.element_create("plain.field", "from", "clam")estyle.layout("EntryStyle.TEntry",                   [('Entry.plain.field', {'children': [(                       'Entry.background', {'children': [(                           'Entry.padding', {'children': [(                               'Entry.textarea', {'sticky': 'nswe'})],                      'sticky': 'nswe'})], 'sticky': 'nswe'})],                      'border':'2', 'sticky': 'nswe'})])estyle.configure("EntryStyle.TEntry",    fieldbackground="light blue")           # Set color hereentry = ttk.Entry(root_window, style="EntryStyle.TEntry")entry.pack(padx=10, pady=10)root_window.mainloop()


Yes, it is possible, you can make your default themes and assign the widgets these themes. What you're looking for is the Style option.

I learnt pretty much everything I needed to know about styles from this :https://docs.python.org/3/library/tkinter.ttk.html#tkinter.ttk.Style

Here's a small example that should give you the basic idea

import tkinterfrom tkinter import ttkroot = tkinter.Tk()ttk.Style().configure("Blue.TEntry", background="blue")blue_ent= ttk.Entry(text="Test", style="Blue.TEntry").pack()root.mainloop()

This gives a good description of how to use ttk.Style() aswell:http://www.tkdocs.com/tutorial/styles.html


import tkinterfrom tkinter import ttkroot = tkinter.Tk()estyle = ttk.Style()estyle.configure("Blue.TEntry", background="blue", fieldbackground="light blue")estyle.layout("Blue.TEntry",                   [('Entry.plain.field', {'children': [(                       'Entry.background', {'children': [(                           'Entry.padding', {'children': [(                               'Entry.textarea', {'sticky': 'nswe'})],                      'sticky': 'nswe'})], 'sticky': 'nswe'})],                      'border':'2', 'sticky': 'nswe'})])blue_ent= ttk.Entry(text="Test", style="Blue.TEntry")blue_ent.pack()root.mainloop()