如何在tkinter中为Entry添加占位符?

时间:2015-01-07 13:04:38

标签: python user-interface tkinter

我在tkinter中创建了一个登录窗口,其中有两个Entry字段,第一个是Username,第二个是Password。
代码

from tkinter import *

ui = Tk()

e1 = Entry(ui)
#i need a placeholder "Username" in the above entry field
e1.pack()

ui.mainloop()

我想要一个名为"用户名"的占位符在Entry中,如果你点击内部,文字应该消失。

7 个答案:

答案 0 :(得分:11)

您可以创建一个继承自Entry的类,如下所示:

import tkinter as tk

class EntryWithPlaceholder(tk.Entry):
    def __init__(self, master=None, placeholder="PLACEHOLDER", color='grey'):
        super().__init__(master)

        self.placeholder = placeholder
        self.placeholder_color = color
        self.default_fg_color = self['fg']

        self.bind("<FocusIn>", self.foc_in)
        self.bind("<FocusOut>", self.foc_out)

        self.put_placeholder()

    def put_placeholder(self):
        self.insert(0, self.placeholder)
        self['fg'] = self.placeholder_color

    def foc_in(self, *args):
        if self['fg'] == self.placeholder_color:
            self.delete('0', 'end')
            self['fg'] = self.default_fg_color

    def foc_out(self, *args):
        if not self.get():
            self.put_placeholder()

if __name__ == "__main__": 
    root = tk.Tk() 
    username = EntryWithPlaceholder(root, "username")
    password = EntryWithPlaceholder(root, "password", 'blue')
    username.pack()
    password.pack()  
    root.mainloop()

答案 1 :(得分:8)

您需要为此条目设置默认值。像这样:

from tkinter import *

ui = Tk()

e1 = Entry(ui)
e1.insert(0, 'username')
e1.pack()

ui.mainloop()

然后,如果要在单击条目时删除内容,则必须使用事件处理程序方法绑定鼠标单击事件以更新此条目的内容。 这是link给你的。

答案 2 :(得分:1)

这将适用于您想要的任何占位符。

from tkinter import *
root = Tk()

my_entry = Entry(root, width=50)
my_entry.pack()
my_entry.insert(0, "Place Holder")
my_entry.configure(state=DISABLED)

def on_click(event):
    my_entry.configure(state=NORMAL)
    my_entry.delete(0, END)

    # make the callback only work once
    my_entry.unbind('<Button-1>', on_click_id)

on_click_id = my_entry.bind('<Button-1>', on_click)

root.mainloop()

答案 3 :(得分:0)

有效的占位符类。它的作用是将其绑定到<FocusIn><FocusOut>上,这样,如果您没有文字时将焦点放在它上,则会将placeholder插入其中。您也可以更改颜色(如果未选择)。

class Placeholder:
    def __init__(self,master,placeholder='',placeholdercolor='grey',color='black',**kwargs):
        self.e = Entry(master,fg=placeholdercolor,**kwargs)
        self.e.bind('<FocusIn>',self.focus_in)
        self.e.bind('<FocusOut>',self.focus_out)
        self.e.insert(0, placeholder)
        self.placeholder = placeholder
        self.placeholdercolor=placeholdercolor
        self.color = color

    def pack(self,side=None,**kwargs):
        self.e.pack(side=side,**kwargs)

    def place(self,side=None,**kwargs):
        self.e.place(side=side,**kwargs)

    def grid(self,column=None,**kwargs):
        self.e.grid(column=column,**kwargs)

    def focus_in(self,e):
        if self.e.get() == self.placeholder:
            self.e.delete(0,END)
        self.e.configure(fg=self.color)

    def focus_out(self,e):
        if self.e.get() == '':
            self.e.configure(fg=self.placeholdercolor)
            self.e.delete(0,END)
            self.e.insert(0,self.placeholder)

答案 4 :(得分:0)

对于比上面列出的更紧凑的解决方案,我建议您创建一个函数来擦除单击事件 (lambda) 上的文本框,如下所示。

from tkinter import *   

def clear_entry(event, entry):
    entry.delete(0, END)
    entry.unbind('<Button-1>', click_event)

ui = Tk()

entry = Entry(ui)

entry.pack()
placeholder_text = '<enter-placeholder>'
entry.insert(0, placeholder_text)

entry.bind("<Button-1>", lambda event: clear_entry(event, entry))

ui.mainloop()

<Button-1>”代表当你左键点击输入框时,所以不要改变它,一旦你点击输入框,它就会触发事件并运行函数 clear_entry。您必须在定义 entry 和使用 placeholder_text 之前声明函数和 entry.insert 元素。希望这是解决此问题的可行解决方案。

答案 5 :(得分:0)

我的解决方案是将 tk.Entry 子类化并控制内容和颜色,将 <FocusIn><FocusOut> 事件绑定到根据需要填充和清除文本的方法。这是行为:

enter image description here

这里是完整的示例代码:

import tkinter as tk

class PlaceholderEntry(tk.Entry):
    def __init__(self, master=None, placeholder='', cnf={}, fg='black',
                 fg_placeholder='grey50', *args, **kw):
        super().__init__(master=None, cnf={}, bg='white', *args, **kw)
        self.fg = fg
        self.fg_placeholder = fg_placeholder
        self.placeholder = placeholder
        self.bind('<FocusOut>', lambda event: self.fill_placeholder())
        self.bind('<FocusIn>', lambda event: self.clear_box())
        self.fill_placeholder()

    def clear_box(self):
        if not self.get() and super().get():
            self.config(fg=self.fg)
            self.delete(0, tk.END)

    def fill_placeholder(self):
        if not super().get():
            self.config(fg=self.fg_placeholder)
            self.insert(0, self.placeholder)
    
    def get(self):
        content = super().get()
        if content == self.placeholder:
            return ''
        return content

class App(tk.Frame):
    def __init__(self, master=None):
        self.root = master
        super().__init__(master, borderwidth=0, relief=tk.RAISED)
        
        self.root.title('Placeholder example')
        self.pack_propagate(False)
        self.pack()
        
        self.entry = PlaceholderEntry(self.root, placeholder='This text is a placeholder')
        self.entry.pack()
        
        self.btn = tk.Button(self.root, text='Nothing', highlightcolor='cyan')
        self.btn.pack()
        

root = tk.Tk()
app = App(master=root)

app.root.mainloop()

答案 6 :(得分:-1)

from tkinter import *

root=Tk()
root.geometry("300x200+600+250")
root.config(background="#E0FFFF")
root.resizable(False,False)


def userText(event):
    e1.delete(0,END)
    usercheck=True

def passText(event):
    e2.delete(0, END)
    passcheck=True



a=StringVar()
b=StringVar()
usercheck=False
passcheck=False


Label(root,text="User name",bg="#E0FFFF").place(x=20,y=50)
e1= Entry(root,textvariable=a)
e1.place(x=100,y=50)
e1.insert(0,"Enter username")
e1.bind("<Button>",userText)


Label(root,text="Password",bg="#E0FFFF").place(x=20,y=95)
e2= Entry(root,textvariable=b)
e2.place(x=100,y=95)
e2.insert(0,"Enter password")
e2.bind("<Button>",passText)


root.mainloop()