Bloo
Bloo

Reputation: 23

Changing background color of ttk entry widget

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.

Upvotes: 1

Views: 3608

Answers (2)

toyota Supra
toyota Supra

Reputation: 4560

You can use style.theme_use('clam') and then add fieldbackground in the 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

Upvotes: 3

Mike Cash
Mike Cash

Reputation: 317

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")

Upvotes: 1

Related Questions