Ttk Input Background Color

How can I change the background color of the Entry widget from ttk? What I still have:

self.estyle = ttk.Style() self.estyle.configure("EntryStyle.TEntry", background='black') self.estyle.map("EntryStyle.TEntry", foreground=[('disabled', 'yellow'), ('active', 'blue')], background=[('disabled', 'magenta'), ('active', 'green')], highlightcolor=[('focus', 'green'), ('!focus', 'red')]) self.urlentry_v = StringVar() self.urlentry = ttk.Entry(self.input_frame, style="EntryStyle.TEntry", textvariable=self.urlentry_v) 

Basically, I changed everything that I can think of, but the text recording remains stubbornly white.

Also, is there a way to change the border color?

+6
source share
2 answers

I realized after a lot of digging. No matter how hard I try to find this, I believe that others will benefit from this:

The standard style applied to ttk.Entry simply does not accept the fieldbackground parameter to change the color of the text input field. The solution is to create a new element that responds to this option.

 from tkinter import * from tkinter import ttk root_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", background="green", foreground="grey", fieldbackground="black") entry_v = StringVar() entry = ttk.Entry(root_window, style="EntryStyle.TEntry", textvariable=entry_v) entry.pack(padx=10, pady=10) 

Unfortunately, it seems that the only way to change the color of the border is to either give it a zero border width, or embed it in a frame that acts as its border, or define a new layout element that uses the image as a border.

Also, note that background control is a very small corner space; if you squint, you can see one pixel of green in each corner.

To use the image as a frame, you can do this:

 img2 = PhotoImage("entryBorder", data=""" R0lGODlhHQAdAOMNAAAAAAQGCAgLERkfLR0mODBFZTFFZTNIajtTezxTez1XgD5XgU Fch////////////ywAAAAAHQAdAAAEbHCQg5i9OGt0iFRaKGLKxBgCoK5s6woGc4Cp a9+AwFQM7ruYn1AVHP6KRhwyaVsyW87nKioFUKXXZ5a5TXaN32FYOD5eqsAzmlX2tZ XqNZGxYATkgAD9wCjUqgIFMgR1I4YZCx4TCYeGCR0DEQA7""") oestyle = ttk.Style() oestyle.element_create("blueborder", "image", "entryBorder", border=3, sticky="nsew") oestyle.layout("OEntryStyle.TEntry", [('Entry.blueborder', {'children': [( 'Entry.padding', {'children': [( 'Entry.textarea', {'sticky': 'nswe'})], 'sticky': 'nswe'})], 'sticky': 'nswe'})]) oestyle.configure("OEntryStyle.TEntry", background="black", foreground="grey") oentry_v = StringVar() oentry = ttk.Entry(root_window, style="OEntryStyle.TEntry", textvariable=oentry_v) oentry.pack(padx=10, pady=10) 

A character string is generated by supplying an image of the borders that I want as a gif so that

 import base64 with open('otherframeBorder.gif', 'rb') as f: encoded = base64.encodestring(f.read()) print(encoded.decode('latin1')) 
+16
source

The code below works fine for me on iMac with Python 3.3.2 and Tcl / Tk 8.5. Also works on Mac G5 with Python 3.3.2 and Tcl / Tk 8.4. It does NOT work with Windows XP sp3 with Python 3.3.2 and Tcl / Tk 8.5. In the latter case, the input background, as for you, remains stubbornly white.

This is why it does not change colors in Windows. An example is also in Tcl.

https://groups.google.com/forum/#!topic/comp.lang.tcl/RmbiFrTFCdw

I have worked a little on your non-working example, and it also works on my Mac, except that I do not get a response from the "active" and "tricks" on the map. Nothing happens.

Some of them are still very mysterious to me. There seems to be some help here:
http://wiki.tcl.tk/38127
http://wiki.tcl.tk/37973
but he is in Tcl and assumes some knowledge on the part of the viewer for reading only.

Here is my example that I mentioned at the beginning of this post:

 from tkinter import * from tkinter.ttk import * class App(Frame): def __init__(self, parent): super().__init__(parent) s = Style() s.configure('Pink.TEntry', background='hot pink') self.e1 = Entry(self) print("Our entry is of type {}".format(type(self.e1))) b = Button(self, text="Switch Styles", command=self.switch) self.pink = False self.e1.pack() b.pack() def switch(self): if self.pink: print("going white", end="") self.e1["style"] = "TEntry" else: print("going pink", end="") self.e1["style"] = "Pink.TEntry" self.pink = not self.pink print("; style is now {}".format(self.e1["style"])) root = Tk() a = App(root) a.pack() root.mainloop() 
+1
source

All Articles