1

I'm trying to change the background color of a ttk entry widget. I read this post ttk Entry background colour but I don't quite understand the element create stuff. Plus, its quite old. So I thought I'd ask here if there's an easier way to change the background color of a ttk widget or if there isn't, then what would I do to change it?

My current code is simply defining an entry widget and setting its background like this:

colorEntry = ttk.Entry(root, background='black')

I've also used styles but that hasn't worked either.

style = ttk.Style()
style.configure("TEntry", background='black')

Both these methods don't do anything to the background. If I try to change any other property like foreground, they work. I'm on windows 10 and using python 3.8.3.

Bloo
  • 13
  • 4
  • If you want to change the color of the entry field (the white part), then you need to use the style option `fieldbackground='black'`, not `background`. But I am not sure the default windows theme allows to change this color, you might have to use a different theme, e.g. 'clam'. – j_4321 Jan 31 '22 at 15:04
  • @j_4321 Thanks! Using a different theme worked. – Bloo Jan 31 '22 at 16:58

2 Answers2

2

Use style.theme_use('clam') and add fieldbackground in Entry parameter.

Snippet:

import tkinter as tk
from tkinter import ttk


root = tk.Tk()
root.geometry("700x350")

style= ttk.Style()
style.theme_use('clam')
style.configure("TEntry", fieldbackground="red")

colorEntry = ttk.Entry(root)
colorEntry.pack(pady=30)
 

root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
1

When creating a Style() widget you also need to apply it to the widget you want to change style to.

colorEntry = ttk.Entry(root, style="TEntry")
Mike Cash
  • 317
  • 4
  • 13
  • 1
    I believe you don't have to do that for default style names like TEntry. However if you create another name like name.TEntry then you have to apply it. – Bloo Jan 31 '22 at 14:55
  • You might be right, but is a good idea to have a custom style just in case you would want multiple entry widgets, you can also just use a Text widget and just change the background color in configure(), is there anything in particular that you would want to use an Entry widget over a Text widget? – Mike Cash Jan 31 '22 at 15:02
  • Not really, but I've made a lot of my program using Entry widgets and changing it all would be a pain. In the end, if I have to I might, however getting a solution to this would be nice. – Bloo Jan 31 '22 at 15:07