【问题标题】:Python Tkinter Entry input and placing PlaceholderPython Tkinter Entry 输入和放置占位符
【发布时间】: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”,然后传入前景等参数。只需定义样式,它们都会遵循它

标签: python tkinter


【解决方案1】:

试试这个: (得到了How to get FLAT relief Entry widget in python ttk?的一些帮助)

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["foreground"]="#000000"

    def _add_placeholder(self, e):
        if not self.get():
            self.insert("0", self.placeholder)
            self["foreground"]="#d5d5d5"

#   declare style variable
s = ttk.Style()
#   assume that classic theme in use
s.theme_use('classic')

#   configure relief
s.configure('Placeholder.TEntry', relief='flat', background="white")
#   lets try to change this structure

s.layout('Placeholder.TEntry', [
    ('Entry.highlight', {
        'sticky': 'nswe',
        'children':
            [('Entry.border', {
                'border': '0',
                'sticky': 'nswe',
                'children':
                    [('Entry.padding', {
                        'sticky': 'nswe',
                        'children':
                            [('Entry.textarea',
                              {'sticky': 'nswe'})]
                    })]
            })]
    })])

self.user_input = PlaceholderEntry(root, 'USERNAME OR EMAIL', font=("Corbel", 15), foreground="#d5d5d5")
self.user_input.place(relx=0.18, rely=0.455, width=300, height=24)

【讨论】:

    猜你喜欢
    • 2021-01-17
    • 2020-04-12
    • 1970-01-01
    • 2020-12-18
    • 2018-02-09
    • 1970-01-01
    • 2017-05-11
    • 2020-09-30
    • 2014-09-27
    相关资源
    最近更新 更多