我正在开发一个带有输入小部件的程序。当用户单击按钮并且该条目小部件为空时,程序会将其边框颜色更改为红色。但当我尝试时,边框保持相同的颜色,即黑色。
这是代码:
self.timeField = Entry(self.mfr, width=40, relief=SOLID, highlightbackground="red", highlightcolor="red")
self.timeField.grid(row=0, column=1, sticky=W)
然后在检查是否为空的 if 语句中将其更改为红色,但似乎不起作用:
self.timeField.config(highlightbackground="red")
self.timeField.config(highlightcolor="red")
有人可以向我解释为什么这不起作用、我做错了什么以及解决方法吗?预先感谢。
更新: 这是所要求的其余代码:
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")
虽然这是一个老问题,但我在 Windows 10 上偶然发现了同样的问题,同时有一个相当简单的解决方案。
除了设置突出显示背景和颜色之外,您还必须将突出显示厚度更改为大于零的值。 Bryan Oekley 在他的答案的标题中提到了它,但我在他的代码中找不到它,所以这里有一个小代码片段。
self.entry = tk.Entry(self, highlightthickness=2)
self.entry.configure(highlightbackground="red", highlightcolor="red")
(这可能应该是对布莱恩斯答案的评论)
您给出的代码应该可以工作,尽管您可能会遇到平台实现的问题(即:Windows 可能不会像其他平台一样对待突出显示厚度)
这是一个应该可以工作的程序,尽管我还没有在 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()
另一个解决方案是创建一个比按钮稍大的边框。这是一个简单的例子。它还没有真正准备好投入生产,但它说明了这一点:
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()