簡體   English   中英

如何在 Python Tkinter 中更改條目小部件邊框顏色

[英]How to change a entry widgets border color in Python Tkinter

我正在開發一個具有入口小部件的程序。 當用戶單擊按鈕並且該條目小部件為空時,如果邊框顏色為紅色,則程序將更改邊框顏色。 但是當我嘗試邊框時,邊框顏色保持不變。 哪個是黑色的。

這是代碼:

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

解決highlightthickness的問題

您提供的代碼應該可以工作,但您可能會遇到平台實現(即:windows 可能不會像其他平台一樣對待 highlightthickness)

這是一個應該可以工作的程序,盡管我還沒有在 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()

雖然這是一個老問題,但我在 Windows 10 上偶然發現了同樣的問題,同時還有一個相當簡單的解決方案。

除了設置高亮背景和顏色之外,您還必須將 highlightthickness 更改為更大的零。 Bryan Oekley在他的回答的標題中提到了它,但我在他的代碼中找不到它,所以這里有一個小代碼片段。

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

(這可能是對 Bryans Answer 的評論)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM