ttk Entry background colour

后端 未结 3 1821
暖寄归人
暖寄归人 2020-12-16 03:31

How exactly do I change the background colour of an Entry widget from ttk? What I have so far is:

        self.estyle = ttk.Style()
        self.estyle.confi         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 03:56

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

    Here's why it doesn't change colors in Windows. The example there is also in Tcl.

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

    I worked on your nonworking example a little, and it also works on my Mac, except that I get no response from "active" and "focus" in the map. Nothing happens there.

    Some of this is still very mysterious to me. It looks like there is some help here:
    http://wiki.tcl.tk/38127
    http://wiki.tcl.tk/37973
    but it's in Tcl and assumes some knowledge on the part of the viewer just to read it.

    Here's 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()
    

提交回复
热议问题