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

2024-09-26 17:55:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个程序,它有一个入口小部件。当用户点击一个按钮,输入窗口小部件为空时,如果边框颜色变为红色,程序就会改变边框颜色。但当我尝试时,边界保持不变。那是黑色的。

代码如下:

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")

Tags: selfconfiggetifredrootpasswordelse
1条回答
网友
1楼 · 发布于 2024-09-26 17:55:19

以高光厚度解决问题

您给出的代码应该可以工作,尽管您可能会碰到平台实现(即:windows可能不会像对待其他平台一样对待高光厚度)

这是一个应该可以工作的程序,尽管我还没有在Windows7上测试过它:

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()

相关问题 更多 >

    热门问题