【发布时间】:2021-06-20 13:30:25
【问题描述】:
我正在使用 Python Tkinter 并收到一条错误消息,提示“_tkinter.TclError:未知选项“-borderwidth”。当我删除边框宽度时,输入框周围有一个边框。我不希望它周围有任何边框。有没有办法让没有边框?我怎样才能让占位符变成灰色,文字变成黑色。
请看下面的代码。我正在使用此代码,以便在输入框中有一个占位符,如果我单击它,占位符将被删除,然后我再次单击它。谢谢
class PlaceholderEntry(ttk.Entry):
def __init__(self, container, placeholder, *args, **kwargs):
super().__init__(container, *args, style="Placeholder.TEntry", **kwargs)
self.placeholder = placeholder
self.insert("0", self.placeholder)
self.bind("<FocusIn>", self._clear_placeholder)
self.bind("<FocusOut>", self._add_placeholder)
def _clear_placeholder(self, e):
if self["style"] == "Placeholder.TEntry":
self.delete("0", "end")
self["style"] = "TEntry"
def _add_placeholder(self, e):
if not self.get():
self.insert("0", self.placeholder)
self["style"] = "Placeholder.TEntry"
self.user_input = PlaceholderEntry(root, 'USERNAME OR EMAIL', font=("Corbel", 15),
foreground="#d5d5d5", borderwidth="0")
self.user_input.place(relx=0.18, rely=0.455, width=300, height=24)
【问题讨论】:
-
您无法更改 ttk 条目的边界(据我所知)。同样在您自己的代码中,您将条目的样式设置为“Placeholder.TEntry”,然后传入前景等参数。只需定义样式,它们都会遵循它