Adding placeholders to tkinter Entry widget in a procedural way

后端 未结 4 1325
独厮守ぢ
独厮守ぢ 2021-01-25 13:20

I know this Q has been answered in this site, but im looking for a more simpler answer, and ive seen one before but then the question has been deleted or something, I cant find

4条回答
  •  长发绾君心
    2021-01-25 14:19

    I have tried this:

    from tkinter import *
    
    root = Tk()
    
    def remove(event):
        if e.get() == 'PLACEHOLDER': #Check default value
            e.delete(0, END)
    
    def add(event):
        if not e.get(): #Check if left empty
            e.insert(0, 'PLACEHOLDER')     
    
    e = Entry(root)
    e.insert(0, 'PLACEHOLDER')
    e.pack(padx=100,pady=(30,0))
    e.bind('', remove)
    e.bind('', add)
    
    e2 = Entry(root)
    e2.pack( pady=(20,100))
    
    root.mainloop()
    

    By doing this you will be clearing only if the default value is present in the Text and also, if the field is left empty, the placeholder gets back into the Text.

提交回复
热议问题