2

I am working on a program that has a entry widget. And when the user clicks a button and that entry widget is empty then the program will change the border color of it to red. But when I try the border just stays the same color, which is black.

Here is the code:

self.timeField = Entry(self.mfr, width=40, relief=SOLID, highlightbackground="red", highlightcolor="red")
self.timeField.grid(row=0, column=1, sticky=W)

Then in the if statement that checks if it is empty has this to change it to red but it does not seem to work:

self.timeField.config(highlightbackground="red")
self.timeField.config(highlightcolor="red")

Can someone explain to me why this is not working, what I am doing wrong, and a way to fix it? Thanks in advance.

Update: Here is the rest of the code as requested:

def start(self):
    waitTime = self.timeField.get()
    password = self.passField.get()

    cTime = str(self.tVers.get())
    self.cTime = cTime

    if waitTime.strip() != "":
        if password.strip() != "":
            if waitTime.isdigit():
                if self.cTime == "Secs":
                    waitTime = int(waitTime)
                elif self.timeVer == "Mins":
                    waitTime = int(waitTime) * 60
                else:
                    waitTime = int(waitTime) * 3600

                self.password = password

                root.withdraw()
                time.sleep(float(waitTime))
                root.deiconify()
                root.overrideredirect(True)
                root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))

                self.tfr.destroy()
                self.mfr.destroy()
                self.bfr.destroy()

                self.create_lockScreen()
            else:
                self.timeField.configure(highlightcolor="red")
        else:
            self.passFields.configure(highlightcolor="red")
    else:
        self.timeField.config(highlightbackground="red", highlightcolor="red")
nayfaan
  • 158
  • 1
  • 2
  • 13
  • Maybe the if statement is never executing. How large is your code? Can you post the whole thing? – Kevin Jan 07 '15 at 18:08
  • @Kevin The statement is executing because I had it say print after the config part and it printed. So it has to be something else. –  Jan 07 '15 at 18:09
  • You might have missed the second half of my previous comment, since I edited it in rather late. How large is your code? Can you post the whole thing? – Kevin Jan 07 '15 at 18:24
  • Sorry for being unclear. When I said the "whole thing", I meant "the entire program" rather than "the entire function". I would like to be able to copy and paste your code and run it, and see the problem. – Kevin Jan 07 '15 at 19:41

2 Answers2

4

Altough it is an old question i stumbled upon the same problem on windows 10 and there is meanwhile a fairly simple solution.

In addition to setting the highlightbackground and color you have to change the highlightthickness to something greater zero. Bryan Oekley mentioned it in the heading of his answer but i couldnt find it in his code so here is a little code snippet.

self.entry = tk.Entry(self, highlightthickness=2)
self.entry.configure(highlightbackground="red", highlightcolor="red")

(this should maybe be a comment on Bryans Answer)

BanjoBenjo
  • 98
  • 8
3

Addressing the issue with highlightthickness

The code you've given should work, though you may be bumping up against platform implementation (ie: windows may not treat the highlightthickness the same as other platforms)

Here's a program that should work, though I haven't tested it on windows 7:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Validate", command=self.validate)
        self.entry.pack(side="top", fill="x")
        self.button.pack(side="bottom")

        self.validate() # initialize the border

    def validate(self):
        data = self.entry.get()
        if len(data) == 0:
            self.entry.configure(highlightbackground="red", highlightcolor="red")
        else:
            self.entry.configure(highlightbackground="blue", highlightcolor="blue")


if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

Using a frame to simulate a border

Another solution is to create a border with a frame that is just slightly larger than the button. Here's a quick example. Its not really production ready, but it illustrates the point:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.entry = CustomEntry(self)
        self.button = tk.Button(self, text="Validate", command=self.validate)
        self.entry.pack(side="top", fill="x")
        self.button.pack(side="bottom")

        self.validate() # initialize the border

    def validate(self):
        data = self.entry.get()
        if len(data) == 0:
            self.entry.set_border_color("red")
        else:
            self.entry.set_border_color("blue")

class CustomEntry(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent)
        self.entry = tk.Entry(self, *args, **kwargs)
        self.entry.pack(fill="both", expand=2, padx=2, pady=2)

        self.get = self.entry.get
        self.insert = self.entry.insert

    def set_border_color(self, color):
        self.configure(background=color)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I tried the self.entry.configure(highlightbackground="red", highlightcolor="red") in my code and it did not work. I posted the rest of the code so that you can see if anything is wrong with it. –  Jan 07 '15 at 19:27
  • @BurningCode: you ran it exactly as-is, without changing anything? What platform are you running this on? – Bryan Oakley Jan 07 '15 at 20:02
  • Yes I ran it exactly as it is. I am running windows 7 with Python 2.7.9 –  Jan 07 '15 at 23:25
  • The meaning of 'focus hightlight' may well depend on the system. I ran on Win 7, 3.4. If I type something, tab to the button and back to the entry, the text gets a blue background and typing anything erases the blued text and replaces it with the character typed. Moving focus back with a mouse click does not blue the text but just adds the cursor, and any key hit is appended. – Terry Jan Reedy Jan 08 '15 at 00:22
  • @BurningCode: nope. Probably is a limitation or bug on windows – Bryan Oakley Jan 08 '15 at 13:56
  • @BryanOakley Is there another way I could make it look like the border turned red? –  Jan 08 '15 at 15:16
  • @BurningCode: yes, you can put a frame behind the button and make it just a couple of pixels wider and taller than the button. – Bryan Oakley Jan 08 '15 at 15:40
  • @BryanOakley Thanks the using the Frame as a border worked. Can you change your answer to where you use a Frame as a border so I can accept it? Thanks –  Jan 08 '15 at 18:11