如何在 Python Tkinter 中更改条目小部件边框颜色

3 python tkinter border widget tkinter-entry

我正在开发一个具有条目小部件的程序。当用户单击按钮并且该条目小部件为空时,程序会将其边框颜色更改为红色。但是当我尝试时,边框保持相同的颜色,即黑色。

这是代码:

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

然后在检查它是否为空的 if 语句中将其更改为红色,但它似乎不起作用:

self.timeField.config(highlightbackground="red")
self.timeField.config(highlightcolor="red")
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么这不起作用、我做错了什么以及解决它的方法吗?提前致谢。

更新:这是按照要求的其余代码:

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")
Run Code Online (Sandbox Code Playgroud)

小智 5

虽然这是一个老问题,但我在 Windows 10 上偶然发现了同样的问题,同时有一个相当简单的解决方案。

除了设置突出显示背景和颜色之外,您还必须将突出显示厚度更改为大于零的值。 Bryan Oekley在他的答案的标题中提到了这一点,但我在他的代码中找不到它,所以这里有一个小代码片段。

self.entry = tk.Entry(self, highlightthickness=2)
self.entry.configure(highlightbackground="red", highlightcolor="red")
Run Code Online (Sandbox Code Playgroud)

(这可能应该是对布莱恩斯答案的评论)